【问题标题】:Snapshot.data is null . The method[] was called on nullSnapshot.data 为 null 。方法 [] 在 null 上被调用
【发布时间】:2021-08-22 05:24:02
【问题描述】:

我是 Flutter 的新手,我正在尝试在未来的构建器中调用 listview 构建器中的 api 数据。但我在快照中得到空数据。

我的代码如下:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';



String token;

SharedPreferences sharedPreferences;

class ContinueW extends StatefulWidget {
  @override

  _ContinueWState createState() => _ContinueWState();
}

class _ContinueWState extends State<ContinueW> {


  Future getTokenValue() async {

    SharedPreferences espf = await SharedPreferences.getInstance();

    token = espf.getString('tokenvalue');

    return token;

  }

  Future createUser() async {

    String url ='http://3.137.182.252:8000/continue-watching';
    print('token');

    Map<String, String> headers = {
      'token': token,
    };
     await http.get(url, headers: headers).then((response){
      print('Entered');
      //print(response.body);
      var jre= jsonDecode(response.body);
      return jre;
     });


  }

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      body: Center(

        child: FutureBuilder(

          future: getTokenValue(),
          builder: (BuildContext context,snapshot){
            if(snapshot.hasData){
              return cbuild();

            } else{
              return CircularProgressIndicator();
            }
          },
        ),
      ),

    );
  }
  Widget cbuild(){

    return new FutureBuilder(

      future: createUser(),

    builder: (BuildContext context,snapshot) {

        if(snapshot.hasData) {
          return ListView.builder(
            itemBuilder: (context, index) {

              return ListTile(
                leading: CircleAvatar(
                  backgroundImage: snapshot.data['thumburl'] != null
                      ? NetworkImage(snapshot.data['thumburl'])
                     : Container(),),
                title: Text(snapshot.data[index]['name']),
                subtitle: Text(snapshot.data[index]['description']),
              );
            },
          );
        }
        else{
          return CircularProgressIndicator();
        }
    }
    );
  }
}

【问题讨论】:

    标签: api flutter null snapshot


    【解决方案1】:
    ListTile(
            leading: CircleAvatar(
              backgroundImage: snapshot.data[index]['thumburl'] != null
                  ? NetworkImage(snapshot.data[index]['thumburl'])
                 : Container(),),
            title: Text(snapshot.data[index]['name']),
            subtitle: Text(snapshot.data[index]['description']),
          );
    

    根据您提供的详细信息。您可能缺少 [index] 和 ['thumburl'],

    如果仍然无法正常工作,那么您需要提供 JSON 格式的数据

    【讨论】:

    • 大家好,我已经解决了这个问题。谢谢大家的回答和意见。
    猜你喜欢
    • 2021-08-29
    • 2021-02-26
    • 2021-11-30
    • 2020-11-10
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多