【问题标题】:Data returned from the function comes to snapshot as null从函数返回的数据作为 null 进入快照
【发布时间】:2021-10-02 22:46:27
【问题描述】:
...
Future<List<ProDetails>> getBasket() async {
  sharedPreferences = await SharedPreferences.getInstance();
  String userDetails =
      sharedPreferences.getString(AppConnection.CART_DETAILS.toString());
  print("userDetails : " "${userDetails}");
  Map<String, dynamic> map = json.decode(userDetails);
  print("map result : " + "${map}");
  List<ProDetails> cartList;
  cartList = (json.decode(userDetails) as List)
      .map((i) => ProDetails.fromJson(i))
      .toList();
  return cartList;
}
...

我在这里发送购物车列表并尝试在下面的代码中获取它。

...
          child: FutureBuilder(
            future: getBasket(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              print("Snapshot data : " + "${snapshot.data}");
              if (snapshot.data == null) {
                return Container(
                  child: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
              } else {
                return SingleChildScrollView(
                  child: Column(
                    children: [
                      ListView.builder(
                          itemCount: snapshot.data.length,
                          // physics: const NeverScrollableScrollPhysics(),
                          itemBuilder: (BuildContext context, int index) {
                            return Container(
                              child: Text("test" + "${index}"),
                            );
                          }),
                    ],
                  ),
                );
              }
            },
          )),

...

这里的 snapshot.data 为 null,页面正在加载。

控制台结果如下, ... I/flutter (23990):快照数据:空 I/flutter (23990): userDetails : [{"productId":"117","productName":"MACC Tea Master
混合 40 袋","category_name":"01 盒 (40
Bags)","price":"1.00","quantity":"1","imgUrl":"1605262901.jpg"}] ... I/flutter (23990):快照数据:null

【问题讨论】:

  • 添加ProDetailsProDetails.fromJson方法的细节。
  • 尝试更改if (snapshot.data == null) insted of if (snapshot.hasData == null)

标签: function flutter dart parameters snapshot


【解决方案1】:

尝试改变 FutureBuilder 中“future”的值

Future getBasketFuture = getBasket();
FutureBUilder(
  future: getBasketFuture
);

您可以在 initState 上定义 getBasketFuture

【讨论】:

    【解决方案2】:

    我认为你的未来正在投掷,这将导致构建器中的数据为空。

    鉴于您拥有的 JSON,json.decode 应该返回 List&lt;dynamic&gt;。 JSON 对象中列表中的每个项目,因此我们需要将列表中的每个项目转换为 Map&lt;String, dynamic&gt;

    我认为这在给定 JSON 的情况下应该是有效的。

    final json = jsonDecode(data) as List;
    final cartList = json
           .map((i) => ProDetails.fromJson(i as Map<String, dynamic>))
           .toList();
    

    这是我用来验证解码的测试。

    const data = '''
    [{
      "productId":"117",
      "productName":"MACC Tea Master Blend 40 Bags",
      "category_name":"01 Box (40 Bags)",
      "price":"1.00",
      "quantity":"1",
      "imgUrl":"1605262901.jpg"
    }]
    ''';
    
    void main() {
      test('can decode JSON', () async {
        final json = jsonDecode(data) as List;
        final cartList = json
            .map((i) => ProDetails.fromJson(i as Map<String, dynamic>))
            .toList();
    
        expect(cartList[0].productId, '117');
      });
    }
    

    【讨论】:

    • @ImzarAhamed 啊,对不起。我用测试更新了答案。
    猜你喜欢
    • 2020-08-05
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多