如何在整个颤振项目中使用 json 对象之一?
Posted
技术标签:
【中文标题】如何在整个颤振项目中使用 json 对象之一?【英文标题】:How can i use one of the json object in the whole of my flutter project? 【发布时间】:2020-11-13 17:08:39 【问题描述】:我从 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']),
)
),
);
【问题讨论】:
能否请您在问题中包含错误? 能否请您在问题中包含错误? 如果您想在整个项目中使用该项目,请尝试将其存储在本地数据库中,例如:共享偏好或 SQFLite,或者您可以继续通过所有小部件传递该项目。 当我使用 jsonDecode(item)['daily'][0]['name'] 时出现此错误: 我答错了请看。 【参考方案1】:这个错误发生在我使用 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)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
【讨论】:
以上是关于如何在整个颤振项目中使用 json 对象之一?的主要内容,如果未能解决你的问题,请参考以下文章