英文:
flutter Error: Operator '[]' cannot be called on 'Map<dynamic, dynamic>?' because it is potentially null
问题
以下是您的翻译:
我是Flutter的新手,一直在浏览Stackoverflow。最近,我遇到了一个从URL获取数据的问题,即使阅读了也无法理解。
这是我的代码:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class DartPad extends StatefulWidget {
const DartPad({Key? key}) : super(key: key);
@override
State<DartPad> createState() => _DartPadState();
}
class _DartPadState extends State<DartPad> {
Map? three;
Future fetchData() async {
var url = Uri.parse('https://www.thegrowingdeveloper.org/apiview?id=2');
http.Response response;
response = await http.get(url);
setState(() {
three = json.decode(response.body);
});
}
@override
void initState() {
fetchData();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
centerTitle: true,
title: const Text('Dartpad'),
),
body: Text(three['id'].toString()),
);
}
}
这是我收到的错误:
错误:无法在 'Map<dynamic, dynamic>?' 上调用运算符 '[]',因为它可能为null。
lib/dartpad.dart:41
- 'Map' 来自 'dart:core'。
body: Text(three['id'].toString()),
^
我不太明白这个错误出现的原因以及如何修复它。有人可以帮助我吗?
我尝试添加 '!' 但仍然不起作用。
英文:
I am new to Flutter and had been browsing through Stackoverflow. I recently encounter a problem with fetch data from URL and unable to understand even after reading it.
This is my code
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class DartPad extends StatefulWidget {
const DartPad({Key? key}) : super(key: key);
@override
State<DartPad> createState() => _DartPadState();
}
class _DartPadState extends State<DartPad> {
// String? one;
// List? two;
Map? three;
Future fetchData() async {
var url = Uri.parse('https://www.thegrowingdeveloper.org/apiview?id=2');
http.Response response;
response = await http.get(url);
setState(() {
three = json.decode(response.body);
});
}
@override
void initState() {
fetchData();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
centerTitle: true,
title: const Text('Dartpad'),
),
body: Text(three['id'].toString()),
);
}
}
This is the error I received
: Error: Operator '[]' cannot be called on 'Map<dynamic, dynamic>?' because it is potentially null.
lib/dartpad.dart:41
- 'Map' is from 'dart:core'.
body: Text(three['id'].toString()),
^
I don't quite understand where this error is coming and how to fix it. Can someone help me?
I tried adding ! and it still doesn't work
答案1
得分: 1
这是由于null-safety
问题引起的,请尝试给它添加默认值,首先像这样定义你的three
:
Map three = {};
然后像这样使用它:
Text("${three['id'] ?? ''}")
当你等待fetchData
获取其结果时,UI构建,同时three
为空,当它尝试从中获取['id']
时,会抛出异常。
推荐的方法来调用async
函数是使用FutureBuilder
:
class _DartPadState extends State<DartPad> {
Future<Map> fetchData() async {
var url = Uri.parse('https://www.thegrowingdeveloper.org/apiview?id=2');
http.Response response;
response = await http.get(url);
return json.decode(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
centerTitle: true,
title: const Text('Dartpad'),
),
body: FutureBuilder<Map>(
future: fetchData(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading....');
default:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
Map three = snapshot.data ?? {};
return Text("${three['id'] ?? ''}");
}
}
},
),
);
}
}
英文:
This happened because of the null-safety
issue, try add default value to it, first define your three like this:
Map three = {};
then use it like this:
Text("${three['id'] ?? ''}")
when you holding for fetchData
to get its result, ui builds and meanwhile, three
is null and when it try to get ['id']
from it, through an exception.
The recommended approach to call async
function is using FutureBuilder
:
class _DartPadState extends State<DartPad> {
Future<Map> fetchData() async {
var url = Uri.parse('https://www.thegrowingdeveloper.org/apiview?id=2');
http.Response response;
response = await http.get(url);
return json.decode(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
centerTitle: true,
title: const Text('Dartpad'),
),
body: FutureBuilder<Map>(
future: fetchData(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading....');
default:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
Map three = snapshot.data ?? {};
return Text("${three['id'] ?? ''}");
}
}
},
),
);
}
}
答案2
得分: 1
three
变量的类型是 Map?
。Map
后面的 ?
表示变量的值可以是 Map
或 null
。
你不能读取 null['id']
,而且 Flutter 不知道 Map
是否为 null。
为了解决这个问题,你有很多选项,具体取决于你想要做什么,以下是一种可能性:
body: Text(three == null ? 'Loading...' : three['id'].toString()),
这样,当 three
为 null 时,你将获得一个显示 Loading...
的文本。
英文:
The three
variable has a type of Map?
. The ?
after Map
means that the value of the variable can be either Map
or null
.
You can't read null['id']
, and flutter doesn't know weather Map
is or isn't null yet.
To fix this, you have many options depending on what you want to do, here is a possibility:
body: Text(three == null ? 'Loading...' : three['id'].toString()),
This way, you will get a Loading...
text while three
is null
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论