我正在从API提取一个随机报价应用程序并返回一个json,我使用的API刷新后会生成新的报价
我在扑朔迷离的应用程序项目中使用DART实施了该程序,并使用http和provider捕获并处理了从公众发送来的数据。
我想在应用程序中添加刷新功能,并且在实现不起作用后使用pull_to_refresh,在这里我们应该获得新的报价,但报价没有改变,
然后如何克服这个问题,当我下拉菜单进行刷新并且刷新完成后,旧数据将替换为新数据
我的主页代码
PACKAGE_bluez-daemon=y
我的测试提供商
import 'package:myquotes/providers/test_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
Provider.of<TestingProvider>(context, listen: false).getTestingProvider();
}
@override
Widget build(BuildContext context) {
var test = Provider.of<TestingProvider>(context).test;
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.teal,
));
RefreshController _refreshController =
RefreshController(initialRefresh: false);
void _onRefresh() async{
// monitor network fetch
await Future.delayed(Duration(milliseconds: 1000));
// if failed,use refreshFailed()
_refreshController.refreshCompleted();
}
void _onLoading() async{
// monitor network fetch
await Future.delayed(Duration(milliseconds: 1000));
var newTest = Provider.of<TestingProvider>(context).test;
// if failed,use loadFailed(),if no data return,use LoadNodata()
setState(() {
test = newTest;
});
_refreshController.loadComplete();
}
return Scaffold(
body: Container(
padding: EdgeInsets.all(15),
child: SmartRefresher(
header: WaterDropMaterialHeader(),
controller: _refreshController,
onRefresh: _onRefresh,
onLoading: _onLoading,
child: ListView(
children: <Widget>[
Column(
children: <Widget>[
Card (
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Center (
child: Text(test != null ? "${test.quote.body}" : "No Data !!!"),
)
],
),
),
]
),
]
),
),
)
);
}
}
型号
import 'package:myquotes/models/test_model.dart';
import 'package:myquotes/services/api_services.dart';
import 'package:flutter/foundation.dart';
class TestingProvider with ChangeNotifier {
var api = ApiServices();
MyTesting test;
Future<MyTesting> getTestingProvider() async {
final response = await api.client.get("${api.testUrl}/api/qotd/");
if (response.statusCode == 200) {
notifyListeners();
var res = myTestingFromJson(response.body);
test = res;
return res;
} else {
return null;
}
}
}
Main.dart
import 'dart:convert';
MyTesting myTestingFromJson(String str) => MyTesting.fromJson(json.decode(str));
String myTestingToJson(MyTesting data) => json.encode(data.toJson());
class MyTesting {
DateTime qotdDate;
Quote quote;
MyTesting({
this.qotdDate,
this.quote,
});
factory MyTesting.fromJson(Map<String, dynamic> json) => MyTesting(
qotdDate: DateTime.parse(json["qotd_date"]),
quote: Quote.fromJson(json["quote"]),
);
Map<String, dynamic> toJson() => {
"qotd_date": qotdDate.toIso8601String(),
"quote": quote.toJson(),
};
}
class Quote {
int id;
bool dialogue;
bool private;
List<String> tags;
String url;
int favoritesCount;
int upvotesCount;
int downvotesCount;
String author;
String authorPermalink;
String body;
Quote({
this.id,
this.dialogue,
this.private,
this.tags,
this.url,
this.favoritesCount,
this.upvotesCount,
this.downvotesCount,
this.author,
this.authorPermalink,
this.body,
});
factory Quote.fromJson(Map<String, dynamic> json) => Quote(
id: json["id"],
dialogue: json["dialogue"],
private: json["private"],
tags: List<String>.from(json["tags"].map((x) => x)),
url: json["url"],
favoritesCount: json["favorites_count"],
upvotesCount: json["upvotes_count"],
downvotesCount: json["downvotes_count"],
author: json["author"],
authorPermalink: json["author_permalink"],
body: json["body"],
);
Map<String, dynamic> toJson() => {
"id": id,
"dialogue": dialogue,
"private": private,
"tags": List<dynamic>.from(tags.map((x) => x)),
"url": url,
"favorites_count": favoritesCount,
"upvotes_count": upvotesCount,
"downvotes_count": downvotesCount,
"author": author,
"author_permalink": authorPermalink,
"body": body,
};
}
答案 0 :(得分:1)
只需检查我所做的更改
void _onRefresh() async {
// monitor network fetch
await Future.delayed(Duration(milliseconds: 1000));
Provider.of<TestingProvider>(context, listen: false).getTestingProvider();
// if failed,use refreshFailed()
_refreshController.refreshCompleted();
}
让我知道它是否有效。