flatter中的JSON解析
我正在使用颤振应用程序。我获取演示json文件 使用此URL获取JSON值。但是当我尝试添加在资产文件夹中创建的JSON文件和我在代码中添加的文件目录路径时。但它不工作,在我添加的flatter中的JSON解析,json,dart,flutter,Json,Dart,Flutter,我正在使用颤振应用程序。我获取演示json文件 使用此URL获取JSON值。但是当我尝试添加在资产文件夹中创建的JSON文件和我在代码中添加的文件目录路径时。但它不工作,在我添加的pubspec.yaml文件中 资产: -assets/test.json import'包:flatter/services.dart'显示rootBundle Future\u loadAStudentAsset()异步{ 返回await rootBundle.loadString('assets/test.jso
pubspec.yaml
文件中
资产:
-assets/test.json
import'包:flatter/services.dart'显示rootBundle代码>
Future\u loadAStudentAsset()异步{
返回await rootBundle.loadString('assets/test.json');
}
我做错了什么。有人能推荐我吗?如何使用同一项目文件夹中的json文件。如果我的代码有任何错误,请随时告诉我。
这是我的工作代码
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
Map data;
List features; // features object list
void main() async {
data = await getQuakes();
features = data['features'];
//print(_data['features'][0]['properties']);
runApp(new MaterialApp(
title: 'Quakes',
home: new Quakes(),
));
}
class Quakes extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Quakes'),
centerTitle: true,
backgroundColor: Colors.red,
),
body: new Center(
child: new ListView.builder(
itemCount: features.length,
padding: const EdgeInsets.all(15.0),
itemBuilder: (BuildContext context, int position) {
//crating the rows for our listview
if (position.isOdd) return new Divider();
final index = position ~/
2; // we are dividing position by 2 and returning an integer result
var format = new DateFormat.yMMMMd("en_US").add_jm();
//var dateString = format.format(date);
var date = format.format(new DateTime.fromMicrosecondsSinceEpoch(
features[index]['properties']['time'] * 1000,
isUtc: true));
return new ListTile(
title: new Text(
" $date",
//title: new Text("Date: $date",
style: new TextStyle(
fontSize: 15.5,
color: Colors.orange,
fontWeight: FontWeight.w500),
),
subtitle: new Text(
"${_features[index]['properties']['place']}",
style: new TextStyle(
fontSize: 14.5,
fontWeight: FontWeight.normal,
color: Colors.grey,
fontStyle: FontStyle.italic),
),
leading: new CircleAvatar(
backgroundColor: Colors.green,
child: new Text(
"${_features[index]['properties']['mag']}",
style: new TextStyle(
fontSize: 16.5,
fontWeight: FontWeight.bold,
color: Colors.white,
fontStyle: FontStyle.normal),
),
),
onTap: () {
showAlertMessage(
context, "${_features[index]['properties']['title']}");
},
);
}),
),
);
}
void _showAlertMessage(BuildContext context, String message) {
var alert = new AlertDialog(
title: new Text('Quakes'),
content: new Text(message),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: new Text('OK'))
],
);
showDialog(context: context, builder: (context) => alert);
}
}
Future<Map> getQuakes() async {
String apiUrl =
'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson';
http.Response response = await http.get(apiUrl);
return json.decode(response.body);
}
导入'dart:async';
导入“dart:convert”;
进口“包装:颤振/材料.省道”;
将“package:http/http.dart”导入为http;
导入“包:intl/intl.dart”;
地图数据;
列出功能;//功能对象列表
void main()异步{
数据=等待getQuakes();
特征=数据[‘特征’];
//打印(_数据['features'][0]['properties']);
runApp(新材料)PP(
标题:"地震",,
首页:新地震(),
));
}
类震动扩展了无状态控件{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“地震”),
标题:对,
背景颜色:Colors.red,
),
正文:新中心(
子项:新建ListView.builder(
itemCount:features.length,
填充:常数边集全部(15.0),
itemBuilder:(构建上下文,int位置){
//为我们的listview排列行
if(position.isOdd)返回新的分隔器();
最终指数=位置~/
2、 //我们将位置除以2并返回一个整数结果
var format=new DateFormat.yMMMMd(“en_US”).add_jm();
//var dateString=format.format(日期);
var date=format.format(新的DateTime.fromMicrosecondsSincepoch(
功能[索引]['properties']['time']*1000,
isUtc:true);
返回新的ListTile(
标题:新文本(
“$date”,
//标题:新文本(“日期:$Date”,
样式:新文本样式(
字体大小:15.5,
颜色:颜色。橙色,
fontWeight:fontWeight.w500),
),
字幕:新文本(
“${u features[index]['properties']['place']}”,
样式:新文本样式(
字体大小:14.5,
fontWeight:fontWeight.normal,
颜色:颜色。灰色,
fontStyle:fontStyle.italic),
),
领先:新CircleAvatar(
背景颜色:Colors.green,
儿童:新文本(
“${u features[index]['properties']['mag']}”,
样式:新文本样式(
字体大小:16.5,
fontWeight:fontWeight.bold,
颜色:颜色,白色,
fontStyle:fontStyle.normal),
),
),
onTap:(){
showAlertMessage(
上下文“${u features[index]['properties']['title']}”;
},
);
}),
),
);
}
void\u showAlertMessage(BuildContext上下文,字符串消息){
var alert=新建警报对话框(
标题:新文本(“地震”),
内容:新文本(消息),
行动:[
新扁平按钮(
已按下:(){
Navigator.pop(上下文);
},
子项:新文本('OK'))
],
);
showDialog(上下文:上下文,生成器:(上下文)=>警报);
}
}
未来的getQuakes()异步{
字符串APIRL=
'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson';
http.Response-Response=wait-http.get(apirl);
返回json.decode(response.body);
}
首先,导入服务包
import'包:flatter/services.dart'显示rootBundle;
然后,更改此选项:
Future\u loadAStudentAsset()异步{
返回await rootBundle.loadString('assets/test.json');
}
为此:
Future\u loadAStudentAsset()异步{
返回await rootBundle.loadString('assets/test.json');
}
loadString()
方法返回字符串类型的未来
请点击此处:
ur tests.json是否直接位于资产文件夹下,或者它们之间是否有文件夹?您是否也有任何错误?@PeterHaddad获取这些ERORR编译器消息:lib/main.dart:80:10:error:无法将“dart.core::String”类型的值分配给“dart.async::FutureOr”类型的变量。编译器消息:尝试更改左侧右侧,或将右侧强制转换为“dart.async::FutureOr”。编译器消息:return wait rootBundle.loadString('assets/test.json');编译器消息:^
谢谢。当我更改为字符串时。打印(_data[0]['title'])
line获取这些错误String无法分配给我在getQuakes()之后编写的参数类型int
;
方法。