首页 > 解决方案 > NosuchMethodError:在 null 上调用了方法“[]”。收件人:空

问题描述

当我在某些手机上安装我的应用程序时,我不断收到此错误,但它在我的设备上运行顺畅

NosuchMethodError:在 null 上调用了方法“[]”。收件人:空。尝试调用:

这是错误的屏幕截图在此处输入图像描述

这是我的代码

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"));
                }

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

标签: flutterdartflutter-layout

解决方案


推荐阅读