简体   繁体   English

如何在我的整个 flutter 项目中使用 json object 之一?

[英]How can i use one of the json object in the whole of my flutter project?

I'm getting some data from JSON, and I wanna search it in my JSON data to preventing of any error (if the item I look it for, I'm not used it else I use it in my programme).我从 JSON 获取一些数据,我想在我的 JSON 数据中搜索它以防止任何错误(如果我要查找的项目,我没有使用它,否则我在我的程序中使用它)。

this is my code but I don't know why occurs error!这是我的代码,但我不知道为什么会出现错误!

but one more Question: How can I store item like ["daily"][0]["name"] in the whole of my programme, because the (daily) item in my JSON is one type of item I want, another is special features or community or featured.但还有一个问题:我如何在整个程序中存储像["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']),
          )
        ),
    );
  }
}

this error occurs when i'm using 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)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

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

相关问题 intellij logcat仅显示“ [object]”的json对象,如何显示整个json对象? - intellij logcat display json object just “[object]”, how can I display the whole json object? 如何在新的android项目中使用整个android项目 - How can I use whole android project inside a new android project 我如何在我的 flutter 应用程序中使用第二个 firebase 项目? - How can i user second firebase project in my flutter app? 如何使用颤振访问 JSON 对象? - How Can I access JSON Object using flutter? 我如何在钛项目中使用局部图像 - how can i use use local images in my titanium project 我可以使用整个原生项目与原生 UI 反应原生吗? - Can I use the whole native project in react native with native UI? 如何在 Dart\Flutter 中使用我的 JSON 响应 - How to use my JSON response in Dart\Flutter 如何将下载到计算机的图片粘贴到我的 flutter 项目的图像文件夹中? - how can i paste the pictures i downloaded to my computer in the images folder in my flutter project? 如何不仅锁定应用程序而且锁定整个设备? - How can I lock Device not just the App but the whole device in flutter? 如何使用我在 Flutter 项目中为 Android 使用的库找到哪些权限 - How do I find which permissions using libraries I use in my Flutter project for Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM