解析 Future Builder 时出错:TypeError: Cannot read property 'Symbol(dartx._get)' of null

Posted

技术标签:

【中文标题】解析 Future Builder 时出错:TypeError: Cannot read property \'Symbol(dartx._get)\' of null【英文标题】:Error when parsing Future Builder: TypeError: Cannot read property 'Symbol(dartx._get)' of null解析 Future Builder 时出错:TypeError: Cannot read property 'Symbol(dartx._get)' of null 【发布时间】:2021-03-09 23:49:44 【问题描述】:

我正在调用未来的 builder builder 并收到此错误:“TypeError: Cannot read property 'Symbol(dartx.get)' of null”即使我正在获取 json 数据。未来的构建器打印当我调用 json 时,我得到 Error: NoSuch Method Error: invalid member on null:'get' on screen 但是,我不知道哪里出了问题。

编辑 1:userEvents 是可在任何地方访问的类级别变量

这是我的未来:

 FutureBuilder(
              future: getMyEvents(),
              builder: (context, snapshot) 
                if (snapshot.hasError) 
                  return Text('Error: $snapshot.error');
                 else 
                  switch (snapshot.connectionState) 
                    case ConnectionState.done:
                      _events = snapshot.data;
                      print("received my event data");
                      print(snapshot.data);
                      return _eventData();
                      break;
                    case ConnectionState.waiting:
                      CircularProgressIndicator();
                      break;
                    default:
                  
                

                return snapshot.data != null
                    ? _eventData()
                    : _noEvents();
              ),

我的 API:

Future getMyEvents() async 
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var token = prefs.getString('token');
  print('my token is $token');

  var events;
  final Map<String, dynamic> body = 
    'access_token': token,
  ;

  var url = "$server/api/events/my_events/";

  await http.post(url, body: body, headers: 
    "Content-Type": "application/x-www-form-urlencoded"
  ).then((http.Response response) 
    print(response.body);

    var responseData = json.decode(response.body);

    switch (response.statusCode) 
      case (200):
        UserEvents userEvents = UserEvents.fromJson(responseData);
        print('My Events $userEvents.events[0]');
        events = userEvents.events;
        print('Events received');
        print('Events $events');

        break;

      case (500):
        print('No Events In This City');
        return '';
        break;
    
  );

  return userEvents;

这是在响应正文中打印的 JSON 数据:


    "success": [
        
            "id": 1,
            "event_name": "Test Event 1",
            "event_photo": "https://dispensaries.s3.amazonaws.com/event_photo/91dfce63-5f52-4e7e-bb38-a5a2800631cc_preview_gD0YH8V.png",
            "vendor_name": 
                "id": 1,
                "vendor": "Test Vendor 1"
            ,
            "refund_available": true,
            "website": "www.test.com",
            "share_count": 0,
            "check_in_count": 1,
            "street_address": "1231 faker streat",
            "city": "Jersey City",
            "state": "NJ",
            "zipcode": "01010",
            "event_tagline": "test",
            "details": "test",
            "start_date": "2020-11-27",
            "start_time": "22:44:00",
            "end_time": "22:44:00",
            "attendees": [
                
                    "id": 2,
                    "name": "test user",
                    "current_selfie": "https://dispensaries.s3.amazonaws.com/sefies/cbdYoga.jpeg"
                
            ]
        ,
       
    ]

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

因为 userEvents 变量是在 switch 语句中定义的,所以它在此范围之外是未定义的。要么将定义移到块外,要么直接从 case 返回:

    switch (response.statusCode) 
      case (200):
        UserEvents userEvents = UserEvents.fromJson(responseData);
        print('My Events $userEvents.events[0]');
        events = userEvents.events;
        print('Events received');
        print('Events $events');

        return userEvents;

      // ...
    

【讨论】:

我很抱歉不清楚。 userEvents 是一个可在任何地方访问的类级别变量,但即使在 switch 案例中使用 return 语句,我仍然会收到该错误。 那么在 switch 块中创建一个同名的新变量有什么意义:UserEvents userEvents = ...? @一个真正的新手 这是一个解析json数据的类。

以上是关于解析 Future Builder 时出错:TypeError: Cannot read property 'Symbol(dartx._get)' of null的主要内容,如果未能解决你的问题,请参考以下文章

无法在Future Builder中显示解析的JSON

Flutter中从api获取数据时出错

我一直在尝试将数据从 Firestore 检索到 Flutter 的 Future builder 但在第一次构建时我收到错误消息我在刷新后得到数据

REST GET 在 Future Builder 中返回无效参数

如何使用 Flutter 数据模型和 Future Builder

Flutter 'map' 动态调用 null。接收方:'_Future<dynamic>' 的实例 参数:['(dynamic) => Builder' 的实例]