Flutter 如何将列表从异步函数加载到小部件中?
所以我想做的是连接到一个数据库并执行一个返回列表的查询。我想把这个列表加载到我的颤振应用程序中,但我不知道该怎么做。这是我当前的代码:Flutter 如何将列表从异步函数加载到小部件中?,flutter,dart,Flutter,Dart,所以我想做的是连接到一个数据库并执行一个返回列表的查询。我想把这个列表加载到我的颤振应用程序中,但我不知道该怎么做。这是我当前的代码: import 'package:flutter/material.dart'; import 'package:postgres/postgres.dart'; void main(List<String> arguments) async { runApp(MyApp()); } class MyApp extends StatefulWi
import 'package:flutter/material.dart';
import 'package:postgres/postgres.dart';
void main(List<String> arguments) async {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Center(
child: Text(
'YOURCHAT',
style: TextStyle(
letterSpacing: 7,
),
),
),
),
body: FutureBuilder()
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:postgres/postgres.dart”;
void main(列表参数)异步{
runApp(MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:中心(
子:文本(
“你的聊天室”,
样式:TextStyle(
字母间距:7,
),
),
),
),
正文:FutureBuilder()
),
);
}
}
我想将数据加载到文本小部件的主体中
somoane能帮我吗
亲切问候,正确使用Future builder
//The function which calls data from database
Future functionName() async {
var result = await http.get('url');
Chats data = [];
decodeResult = json.decode(result);
for (d in decodeResult){
data = (d["your db column"]: title, d["your db column"]: subtitle)
}
return data;
}
更新了你的身体部分
body: SingleChildScrollView(
child: Futurebuilder(
future: functionName,
builder: (BuildContext context, AsyncSnapshot snashot){
return Listview.builder(
builder: (BuildContext contex, int index){
if (snapshot.hasData){
return ListTile(
title: Text(snapshot.data.title),
subtitle: Text(snapshot.data.subtitle)
);
}
);
}
)
)
body: SingleChildScrollView(
child: Futurebuilder(
future: functionName,
builder: (BuildContext context, AsyncSnapshot snashot){
return Listview.builder(
builder: (BuildContext contex, int index){
if (snapshot.hasData){
return ListTile(
title: Text(snapshot.data.title),
subtitle: Text(snapshot.data.subtitle)
);
}
);
}
)
)