简体   繁体   English

当我尝试从 url 获取数据后尝试运行我的 flutter 应用程序时,出现 null 错误

[英]I get a null error when I try to run my flutter app after trying to fetch data from a url

I get the error below after modifying my home page on my app to the code as shown below, I get the error in my object_patch.dart file.在将我的应用程序主页修改为如下所示的代码后,我收到以下错误,我在我的 object_patch.dart 文件中收到错误。 I don't know how that can be resolved.我不知道如何解决。 what could I have done wrong?我做错了什么?

Exception has occurred.发生异常。 NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: ) (see image) NoSuchMethodError(NoSuchMethodError:在 null 上调用了方法“[]”。接收方:null 尝试调用:) (见图)

在此处输入图像描述

在此处输入图像描述


import 'package:awsome_app/drawer.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class Homepage extends StatefulWidget {
  const Homepage({Key? key}) : super(key: key);

  @override
  State<Homepage> createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  //Functions here now

  // var myText = "This is a function";
  // TextEditingController _nameController = TextEditingController();

  var url = "https://jsonplaceholder.typicode.com/photos";
  var data;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    fetchData();
  }

  fetchData() async {
    var res = await http.post(Uri.parse(url));
    data = jsonDecode(res.body);
    setState(() {});
    ;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(204, 255, 249, 249),
      appBar: AppBar(title: const Text("Welcome to Flutter App")),
      body: data != null
          ? ListView.builder(
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(data[index]["title"]),
                );
              },
              itemCount: data.length,
            )
          : Center(
              child: CircularProgressIndicator(),
            )
    );
  }
}```

GET https://jsonplaceholder.typicode.com/photos will return a list of photos. GET https://jsonplaceholder.typicode.com/photos将返回照片列表。

Replace代替

var res = await http.post(Uri.parse(url));

with

var res = await http.get(Uri.parse(url));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 vscode 上通过 flutter 运行应用程序时出现此错误 - I get this error when trying to run an app via flutter on vscode 当我尝试在 android studio 上运行我的颤振应用程序时,请一直收到此错误 - Please I keep getting this error when I try to run my flutter app on android studio 当我尝试运行时,我的 Flutter 应用程序没有出现在模拟器中 - My Flutter app doesnt appear in the emulator when I try to run it 尝试运行我的应用程序时粘贴JSON时出现问题,我收到以下错误消息:JSONObject jsonObject = new JSONObject(result); - Problem in pasing json when I try to run my app I get error from:JSONObject jsonObject = new JSONObject(result); 当我尝试从Firestore提取数据并显示在listview片段中时,我的应用程序崩溃 - My app crashes when i try to fetch data from Firestore and display in the listview fragment 当我尝试在 android 上运行我的应用程序时出错 - Error when i try to run my app on android 我正在尝试将数据从我的 flutter 应用程序保存到谷歌表格,但我收到格式异常 - i am trying to save data from my flutter app to google sheets ,but i get a format exception 当我使用 Flutter 运行此应用程序时,我收到此错误 - When I run this app with Flutter, I get this error 升级android studio 2.2.2后,运行应用程序时出现错误 - After upgrading my android studio 2.2.2, when I run my app I get an error 尝试从android studio中的数据库中打印数据后,出现空引用异常 - i get a null reference exception after trying to print data from my database in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM