gpt4 book ai didi

json - flutter 下拉刷新

转载 作者:行者123 更新时间:2023-12-04 04:15:02 26 4
gpt4 key购买 nike

我已经实现了这段代码来显示来自 web url 的 json 数据列表。
我试图实现一个简单的下拉刷新,但没有任何效果。

Flutter 代码很长,但实际上非常简单。它有 flutter 的主要类,以及从 web 加载 json 数据的 future 方法。

我只想实现一个简单的下拉刷新。

我在这里错过了什么?

为什么它不起作用?

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:pull_to_refresh/pull_to_refresh.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'XXX',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'XXX'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);


final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

final RefreshController _refreshController = RefreshController();

Future<List<User>> _getUsers() async {

var data = await http.get("xxxxxxxxxxxxx");


if (data.statusCode == 200) {

print('Status Code 200: Ok!');

var jsonData = json.decode(data.body);


List<User> users = [];

for (var k in jsonData.keys) {

var u = jsonData[k];
//print(u["pubdate"]);

User user = User(u["id"], u["source"], u["desc"], u["link"], u["title"], u["img"], u["pubdate"]);

users.add(user);

}

print(users.length);

return users;

} else {

throw Exception('Failed to load json');

}


}

@override
Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SmartRefresher(
controller: _refreshController,
enablePullDown: true,
header: WaterDropHeader(),
onRefresh: () async {
await Future.delayed(Duration(seconds: 1));
_refreshController.refreshCompleted();
},
child: FutureBuilder(

future: _getUsers(),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.data == null){

return Container(
child: Center(
child: Text("Loading..."),
),
);

} else {


return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int id){

return ListTile(

leading: CircleAvatar(
backgroundImage: NetworkImage(
snapshot.data[id].img
),
),

title: Text(snapshot.data[id].title),
subtitle: Column(
children: <Widget>[
Row(
children: [
Text(
snapshot.data[id].source,
overflow: TextOverflow.fade,
maxLines: 1,
softWrap: false,
),
Spacer(),
Text(snapshot.data[id].pubdate),
],
),
],
)
);
},
);

}

},

),
),

);
}
}

class User {

final int id;
final String source;
final String desc;
final String link;
final String title;
final String img;
final String pubdate;

User(this.id, this.source, this.desc, this.link, this.title, this.img, this.pubdate);


}

最佳答案

您必须在 上获得用户正在加载 如下所示

@override
Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SmartRefresher(
....
onLoading: _getUsers,
.....
)
}

关于json - flutter 下拉刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60872615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com