在构建FutureBuilder <dynamic>时引发NoSuchMethodError

时间:2020-07-19 19:57:56

标签: flutter dart

我已经使用颤振一个星期了,我真的不知道自己在做什么, 但是我需要迭代标记数据才能在Google地图上显示,我正在使用虚拟数据来 测试。

import 'dart:async';
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:karage_test_1/app_bar.dart';
import 'package:karage_test_1/drawer.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class FireMap extends StatefulWidget {
@override
  _FireMapState createState() => _FireMapState();
}

class _FireMapState extends State<FireMap> {
  Future _future;
    Future<String> loadString() async =>
       await rootBundle.loadString('assets/data.json');
    List<Marker> allMarkers = [];
    @override
   void initState() {
    super.initState();
    _future = loadString();
  }
 Widget build(BuildContext context) {
    return Stack(
          children: <Widget>[
      Container(
        child:
         FutureBuilder<dynamic> (
            future: _future,
            builder: (context, AsyncSnapshot snapshot) {
               List<dynamic> parsedJson = jsonDecode(snapshot.data);
               print(parsedJson);
               allMarkers = parsedJson.map((e) {
               return Marker(
                    markerId: MarkerId(e['nam']),
                    position: LatLng(e[['coord'][0]], e[['cooord'][1]]));
               }).toList();
              return GoogleMap(
                 initialCameraPosition: CameraPosition(
                 target: LatLng(31.9539, 35.9106),
                 zoom: 15,
               ),
               markers: Set.from(allMarkers),
               );
            },
        ))]);
     } 
    }

我收到此错误:

The following NoSuchMethodError was thrown building FutureBuilder<dynamic>(dirty, state: 
_FutureBuilderState<dynamic>#21889):
The getter 'length' was called on null.
Receiver: null
Tried calling: length

我该如何解决,请记住,飞镖和飞镖是新手

1 个答案:

答案 0 :(得分:0)

jsonDecode()方法似乎存在问题,请同时添加该方法来解决该问题。

首先,snapshot.data为空,并且我认为您正在尝试迭代snapshot.data并引发异常。您也可以调用snapshot.hasData()方法来检查数据是否可用。