【问题标题】:Flutter The argument type 'Future<dynamic>' can't be assigned to the parameter type 'Future<Deposit?>?Flutter 参数类型\'Future<dynamic>\'不能赋值给参数类型\'Future<Deposit?>?
【发布时间】:2022-11-17 18:33:39
【问题描述】:

服务:

static Future addDeposit(String amount, String product, String phone, String token) async {
    assert(token.isNotEmpty);
    try{
      Response<String> response = await _dio.post(
        'url',
          data: <String, String>{
            'amount': amount,
            'product': product,
            'phone': phone,
          
          },

      );
    
      if (response.statusCode == 200){
        return Deposit.fromJson(jsonDecode(response.data  ?? '{}'));
      }else if(response.statusCode == 400){
        return Deposit.fromJson(response.data ?? '');
      }else{
        throw Exception(response.statusMessage);
      }
    }catch (e){
      print(e);
    }
  }
child: FutureBuilder<Deposit?>(
  future: AuthService.addDeposit(amount, product, phone,),
  builder: (BuildContext context, AsyncSnapshot<Deposit?> snapshot){
    if(snapshot.hasData){
      return _snapshotHasData(snapshot.data!);
    }else if(snapshot.hasError){
      return Text('${snapshot.error}');
    }
    return const LinearProgressIndicator();
  },
),

错误:无法将参数类型“Future”分配给参数类型“Future<Deposit?>?”。

  • “Future”来自“dart:async”。
  • “存款”来自“package:ias/models/deposit_model.dart”(“lib/models/deposit_model.dart”)。 未来:AuthService.addDeposit(金额,产品,电话),

【问题讨论】:

  • 你能包括更多关于AuthService.addDeposit的信息吗?

标签: flutter dart dart-null-safety


【解决方案1】:

尝试添加数据返回类型

Future<Deposit?> addDeposit(String amount, String product, String phone, String token) async {

更多关于null-safety

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 2021-12-30
    • 2021-11-23
    • 2021-04-07
    • 2020-10-25
    • 2021-11-24
    • 2021-06-13
    • 2021-07-06
    • 2021-10-16
    相关资源
    最近更新 更多