从杰森读飞镖

时间:2019-12-16 20:01:11

标签: json flutter dart

Dart语言的新手,一直在尝试制作一个应用程序,但是我已经在这个问题上停留了一段时间,并且似乎无法解决它。我一直在尝试从Json文件中读取内容,并使用随机值生成器来选择要显示的Json信息,

ex

名称:Roxxxer 电子邮件:Roxxxer@hotmail.com

名称:Roxxxer1 电子邮件:Roxxxer1@hotmail.com

但是每次我尝试显示其中任何一个值时,都会出现此错误:

enter image description here

这是json类阅读器(?)

class Recept{
  final String title;
  final String body;

  Recept({this.title, this.body});

  factory Recept.fromJson(dynamic json){
    return Recept( title: json['title'] as String,
                    body: json['body'] as String,

    );
  }

  String toString() {
    return '{ ${this.title}, ${this.body} }';
  }


}



import 'dart:convert';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:randomfood/Recept.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(
      new MaterialApp(
          home: new AwesomeButton()
      )
  );
}

class AwesomeButton extends StatefulWidget {
  @override
  Button createState() => new Button();
}

class Button extends State<AwesomeButton> {

  String test;

  void onPressed() {
    setState(() {
      //This is where the random generator goes in and give a new place in the json file
    });
  }

  Future<String> LoadRecipesFromJson() async{
    return await rootBundle.loadString('assets/test.json');
  }

  Future LoadRecipes() async{
    String jsonString = await LoadRecipesFromJson();
    final jsonResponse = json.decode(jsonString);
    Recept recept = new Recept.fromJson(jsonResponse);

    String ToString() {
      return '${recept.title}';
    }

    test = ToString();
  }

  @override
  void initState() {
    super.initState();
    LoadRecipes();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(title: new Text("Random Recipe Generator!"), backgroundColor: Colors.blueGrey),
        body: new Container(
            child: new Center(
                child: new Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,


                    children: <Widget>[

                      new Text(test, style: new TextStyle(color: Colors.black,fontStyle: FontStyle.italic, fontSize: 30.0, fontWeight: FontWeight.bold)),
                      new Padding(padding: new EdgeInsets.all(50.0)),
                      new RaisedButton(
                          child: new Text("Random Recept!", style: new TextStyle(color: Colors.white, fontStyle: FontStyle.italic, fontSize: 20.0)),
                          color: Colors.blueGrey,
                          onPressed: onPressed
                      )
                    ]
                )
            )
        )
    );
  }


}

2 个答案:

答案 0 :(得分:1)

由于您使用的是put,因此即使您在StatefulWidget中调用函数,也需要使用setState()方法来更新变量。您必须这样做的原因是您正在使用异步数据。 initState不会等待数据,因此您必须自己更新状态。更新状态后,它将再次调用initState方法并重建小部件,而您的build小部件将使用Text变量中的新值。

此外,从Dart 2.0开始,test关键字是可选的。

更正:您可以将一个函数放在另一个函数中。

new

答案 1 :(得分:1)

变量 test 开头可能没有值,因为您正在异步加载值到测试变量中。 如果test为null,则在文本(test)小部件内给测试变量 时,会出现一个错误,提示您现在正在获取。 因此,要防止这种情况发生,只需对代码更改进行一点更改

.php

.php

现在,如果test的值为null,则将得到 Loading (加载)作为文本,否则,test的值就是什么。