我从 JSON 中获取一些数据,我想在 JSON 数据中搜索它以防止出现任何错误(如果我查找的项目,我不会使用它,否则我会在我的程序中使用它)。
这是我的代码,但我不知道为什么会出现错误!
还有一个问题:
我如何在整个程序中存储像
["daily"][0]["name"]
这样的项目,因为我的JSON中的(每日)项目是我想要的一种类型的项目,另一种是特殊功能或社区或特色。
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class ItemShop extends StatefulWidget {
static final String id = 'itemShop';
@override
_ItemShopState createState() => _ItemShopState();
}
var item;
class _ItemShopState extends State<ItemShop> {
var item;
Future fetchItems () async{
http.Response response = await http.get('https://fortniteapi.io/shop?lang=en' , headers: {"Authorization" : "97206de0-b1819688-6af9c8d0-d5ded771"});
if (response.statusCode == 200) {
Map<String , dynamic> item = jsonDecode(response.body);
this.setState(() {
item = item;
});
}
}
@override
void initState() {
super.initState();
fetchItems();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ItemShop'),
),
body: Container(
color: Colors.blue,
child: Card(
child: Text(jsonDecode(item)['daily'][0]['name']),
)
),
);
}
}
当我使用 jsonDecode(item)['daily'][0]['name] 时会发生此错误
Restarted application in 1,711ms.
I/flutter ( 3468): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3468): The following NoSuchMethodError was thrown building ItemShop(dirty, state: _ItemShopState#93ef8):
I/flutter ( 3468): The getter 'length' was called on null.
I/flutter ( 3468): Receiver: null
I/flutter ( 3468): Tried calling: length
I/flutter ( 3468): The relevant error-causing widget was:
I/flutter ( 3468): ItemShop file:///D:/ArenaApplicationFlutter/arena/lib/main.dart:15:36
I/flutter ( 3468): When the exception was thrown, this was the stack:
I/flutter ( 3468): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
I/flutter ( 3468): #1 _parseJson (dart:convert-patch/convert_patch.dart:30:28)
I/flutter ( 3468): #2 JsonDecoder.convert (dart:convert/json.dart:505:36)
I/flutter ( 3468): #3 JsonCodec.decode (dart:convert/json.dart:153:41)
I/flutter ( 3468): #4 jsonDecode (dart:convert/json.dart:96:10)
I/flutter ( 3468): #5 _ItemShopState.build (package:arena/Screens/itemShop.dart:51:25)
I/flutter ( 3468): #6 StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
I/flutter ( 3468): #7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
I/flutter ( 3468): #8 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
I/flutter ( 3468): #9 Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
I/flutter ( 3468): #10 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4481:5)
I/flutter ( 3468): #11 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4666:11)
I/flutter ( 3468): #12 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
I/flutter ( 3468): ... Normal element mounting (115 frames)
I/flutter ( 3468): #127 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
I/flutter ( 3468): #128 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5947:32)
I/flutter ( 3468): ... Normal element mounting (253 frames)
I/flutter ( 3468): #381 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
I/flutter ( 3468): #382 Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
I/flutter ( 3468): #383 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1148:16)
I/flutter ( 3468): #384 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1119:5)
I/flutter ( 3468): #385 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1061:17)
I/flutter ( 3468): #386 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2607:19)
I/flutter ( 3468): #387 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1060:13)
I/flutter ( 3468): #388 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:941:7)
I/flutter ( 3468): #389 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:922:7)
I/flutter ( 3468): (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
I/flutter ( 3468): ════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building ItemShop(dirty, state: _ItemShopState#93ef8):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
ItemShop file:///D:/ArenaApplicationFlutter/arena/lib/main.dart:15:36
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 _parseJson (dart:convert-patch/convert_patch.dart:30:28)
#2 JsonDecoder.convert (dart:convert/json.dart:505:36)
#3 JsonCodec.decode (dart:convert/json.dart:153:41)
#4 jsonDecode (dart:convert/json.dart:96:10)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building ItemShop(dirty, state: _ItemShopState#93ef8):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
ItemShop file:///D:/ArenaApplicationFlutter/arena/lib/main.dart:15:36
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 _parseJson (dart:convert-patch/convert_patch.dart:30:28)
#2 JsonDecoder.convert (dart:convert/json.dart:505:36)
#3 JsonCodec.decode (dart:convert/json.dart:153:41)
#4 jsonDecode (dart:convert/json.dart:96:10)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
当我使用 jsonDecode(item)['daily'][0]['name] 时会发生此错误
Restarted application in 1,711ms.
I/flutter ( 3468): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3468): The following NoSuchMethodError was thrown building ItemShop(dirty, state: _ItemShopState#93ef8):
I/flutter ( 3468): The getter 'length' was called on null.
I/flutter ( 3468): Receiver: null
I/flutter ( 3468): Tried calling: length
I/flutter ( 3468): The relevant error-causing widget was:
I/flutter ( 3468): ItemShop file:///D:/ArenaApplicationFlutter/arena/lib/main.dart:15:36
I/flutter ( 3468): When the exception was thrown, this was the stack:
I/flutter ( 3468): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
I/flutter ( 3468): #1 _parseJson (dart:convert-patch/convert_patch.dart:30:28)
I/flutter ( 3468): #2 JsonDecoder.convert (dart:convert/json.dart:505:36)
I/flutter ( 3468): #3 JsonCodec.decode (dart:convert/json.dart:153:41)
I/flutter ( 3468): #4 jsonDecode (dart:convert/json.dart:96:10)
I/flutter ( 3468): #5 _ItemShopState.build (package:arena/Screens/itemShop.dart:51:25)
I/flutter ( 3468): #6 StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
I/flutter ( 3468): #7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
I/flutter ( 3468): #8 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
I/flutter ( 3468): #9 Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
I/flutter ( 3468): #10 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4481:5)
I/flutter ( 3468): #11 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4666:11)
I/flutter ( 3468): #12 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
I/flutter ( 3468): ... Normal element mounting (115 frames)
I/flutter ( 3468): #127 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
I/flutter ( 3468): #128 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5947:32)
I/flutter ( 3468): ... Normal element mounting (253 frames)
I/flutter ( 3468): #381 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
I/flutter ( 3468): #382 Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
I/flutter ( 3468): #383 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1148:16)
I/flutter ( 3468): #384 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1119:5)
I/flutter ( 3468): #385 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1061:17)
I/flutter ( 3468): #386 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2607:19)
I/flutter ( 3468): #387 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1060:13)
I/flutter ( 3468): #388 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:941:7)
I/flutter ( 3468): #389 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:922:7)
I/flutter ( 3468): (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
I/flutter ( 3468): ════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building ItemShop(dirty, state: _ItemShopState#93ef8):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
ItemShop file:///D:/ArenaApplicationFlutter/arena/lib/main.dart:15:36
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 _parseJson (dart:convert-patch/convert_patch.dart:30:28)
#2 JsonDecoder.convert (dart:convert/json.dart:505:36)
#3 JsonCodec.decode (dart:convert/json.dart:153:41)
#4 jsonDecode (dart:convert/json.dart:96:10)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building ItemShop(dirty, state: _ItemShopState#93ef8):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
ItemShop file:///D:/ArenaApplicationFlutter/arena/lib/main.dart:15:36
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 _parseJson (dart:convert-patch/convert_patch.dart:30:28)
#2 JsonDecoder.convert (dart:convert/json.dart:505:36)
#3 JsonCodec.decode (dart:convert/json.dart:153:41)
#4 jsonDecode (dart:convert/json.dart:96:10)
...
════════════════════════════════════════════════════════════════════════════════════════════════════