【问题标题】:FutureBuilder not populating valueFutureBuilder 不填充值
【发布时间】:2021-12-31 16:11:07
【问题描述】:

在我的main.dart 中,我有一个异步函数可以从 URL 获取数据。

getShopLength() async {
  final queryParameters = {
    'api_key': '123',
    'user_id': '123',
    'lat': '123',
    'long': '123',
    'km': '123',
  };
  var response = await http.get(Uri.https('google.de','getSth', queryParameters));
  var jsonData = jsonDecode(response.body);
  List<Shops> shops = [];

  for(var x in jsonData) {
  Shops shop = Shops(x['name'], x['slogan']);
  shops.add(shop);
  }
  return shops.length;
}

在我的home.dart 中,我想从getShopLength() 获取值,但我总是收到错误:type 'Future&lt;dynamic&gt; is not a subtype of type 'Future&lt;String&gt;?'

我尝试将返回值保存到valueShop 并传递给buildRestaurantRow('Top Angebote', context, valueShop)

home.dart

@override
  Widget build(BuildContext context) {

    var valueShop = "0";

    FutureBuilder<String>(
        future: getShopLength(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            valueShop = snapshot.data;
          }
          return CircularProgressIndicator();
        }
    );


    return Scaffold(
      appBar: buildSearchBar(context),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 0),
        child: ListView(
          children: <Widget>[
            SizedBox(height: 20.0),
            buildRestaurantRow('Top Angebote', context, valueShop),
            SizedBox(height: 10.0),
            buildRestaurantList(context),
            SizedBox(height: 10.0),
            buildCategoryRow('Nach Kategorie', context),
            SizedBox(height: 10.0),
            buildCategoryList(context),
            SizedBox(height: 20.0),
            buildCategoryRow('Deine Favoriten', context),
            SizedBox(height: 10.0),
            buildFriendsList(),
            SizedBox(height: 30.0),
          ],
        ),
      ),
    );
  }

我错过了什么?

【问题讨论】:

    标签: flutter flutter-futurebuilder flutter-http


    【解决方案1】:

    所以问题出在这里:

    FutureBuilder<String>(
            future: getShopLength(),
    

    你的future builder有一个字符串类型,这意味着future应该是Future&lt;String&gt;类型,但是当你声明函数getShopLength时,你这样做了:

    getShopLength() async {
    

    你没有给它一个返回类型,因此默认返回类型是Future&lt;dynamic&gt;

    显而易见的解决方案是给函数一个返回类型,但你还有另一个问题:

    futurebuilder 需要一个字符串值,但函数返回一个数字,那么它是什么?

    如果你想返回一个长度的字符串,你可以这样做:

    Future<String> getShopLength() async  {
      ...
      return shops.length.toString();
    }
    

    或者你也可以将futurebuilder的值改为int:

    Future<int> getShopLength() async  {
      ...
      return shops.length;
    }
    ...
    int valueShop = 0;
    
    FutureBuilder<int>(
      future: getShopLength(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          valueShop = snapshot.data;
        }
        return CircularProgressIndicator();
      },
    );
    

    旁注:

    好的,关于您的代码,我有几件事要提一下: 首先,在您的getShopsLength 函数中,您有两个列表,jsonDatashops,您实际上并不需要两者,您可以只使用一个:

    var jsonData = jsonDecode(response.body);
    return jsonData.length // no need for the shops list.
    

    其次,您的构建器代码是怎么回事?您首先声明了一个FutureBuilder,然后完全忽略它并继续使用Scaffold 小部件?我相信脚手架代码应该在未来的构建器中,就目前而言,你永远不会看到循环进度指示器:

    发件人:

    var valueShop = '0';
    
    FutureBuilder<String>(
            future: getShopLength(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                valueShop = snapshot.data;
              }
              return CircularProgressIndicator();
            }
        );
    
    return Scaffold(...);
    

    收件人:

    return FutureBuilder<String>(
      future: getShopLength(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          var valueShop = snapshot.data;
          return Scaffold(...);
        }
        return CircularProgressIndicator();
      }
    );
    

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 2015-07-13
      • 2016-10-10
      • 2020-06-30
      • 1970-01-01
      相关资源
      最近更新 更多