NosuchMethodError:方法'[]'在null上调用。收件人:空

时间:2020-06-08 16:04:21

标签: flutter dart flutter-layout

在某些手机上安装我的应用程序后,我仍然收到此错误,但它在我的设备上运行正常

NosuchMethodError:方法'[]'在null上调用。接收者:null。 尝试致电:

这是错误enter image description here

的屏幕截图

这是我的代码

double long;
 Future<ReverseLocation> rLocation() async {
String request =
   "http://**.***.**.**:****/traffic/location/reverse_geocode/?longitude=$long&latitude=$lat";
final response = await http.get(request);
print(request);
print(response.statusCode);
try{

return ReverseLocation.fromRawJson(response.body);
}
catch(e){
return ReverseLocation.fromRawJson(response.body);
}

这是我的视图

@override
_SearchLocationState createState() => _SearchLocationState();
}

class _SearchLocationState extends State<SearchLocation> {
Future<ReverseLocation> locationtwo;

@override
void initState() {
  super.initState();
  locationtwo = rLocation();
}

Widget build(BuildContext context) {
  return SafeArea(
    child: Container(
      height: double.infinity,
      width: double.infinity,
      child: Scaffold(
        body: SafeArea(
          child: FutureBuilder(
           // initialData: [],
              future: locationtwo,
              builder: (BuildContext context, snapshot) {
                if (snapshot.connectionState == ConnectionState.none) {
                  return NetworkModal();
                } else if (snapshot.connectionState == ConnectionState.done) {
                  if (snapshot.hasData) {
                    return Center(
                        child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(snapshot
                            .data.entity.addressComponents[0].longName),
                      ],
                    ));
                  } else if (snapshot.hasError) {
                    return Center(child: Text("Error ${snapshot.error.toString()}"));

                  }
                  else if(snapshot.data == null) {
                  return Center(child: Text("Null ${snapshot.error}"));
                }
                } {
                  return Center(child: Text("Trying to get Address"));
                }

              }),
        ),
      ),
    ),
  );
}
}

0 个答案:

没有答案