第一次发布,我对编码非常陌生,我正在尝试学习将本地json加载到flutter中的知识。 我按照本教程https://www.youtube.com/watch?v=bTwTKwK3hGc的顺序进行字母和三重检查,没有发现任何差异,并且在编辑器中没有错误显示,但是当我尝试运行代码时,出现“ FormatException”错误。
代码:
import 'package:flutter/material.dart';
import 'dart:convert';
void main() => runApp(new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.teal,
),
home: new HomePage(),
));
class HomePage extends StatefulWidget {
@override
HomePageState createState() => new HomePageState();
}
class HomePageState extends State<HomePage> {
List data;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Load Json Practice"),
),
body: new Container(
child: new Center(
child: new FutureBuilder(
future: DefaultAssetBundle
.of(context)
.loadString('load_json/stuff.json'),
builder: (context, snapshot) {
//decode json:
var mydata = jsonDecode(snapshot.data.toString());
return new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Text("Name: " + mydata[index]['name']),
],
),
);
},
itemCount: mydata == null ? 0 : mydata.length,
);
},
),
),
),
);
}
}
我的Json文件:
[{
"id":1,
"name":"example"
}
]
我的错误:
════════ Exception caught by widgets library ═══════════════════════════════════
The following FormatException was thrown building FutureBuilder<String>(dirty, state: _FutureBuilderState<String>#07cf5):
Unexpected end of input (at character 1)
^
User-created ancestor of the error-causing widget was
Center
lib/main.dart:25
When the exception was thrown, this was the stack
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1392:5)
#1 _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:510:7)
#2 _parseJson (dart:convert-patch/convert_patch.dart:30:10)
#3 JsonDecoder.convert (dart:convert/json.dart:493:36)
#4 JsonCodec.decode (dart:convert/json.dart:151:41)
...
我仍然很难理解我得到的错误,我已经搜集了所有可以想到的尝试自己解决的方法,却不知道如何进一步解决此问题。任何帮助或建议,将不胜感激。谢谢您的宝贵时间。