我正在制作一个从API获取并返回 json 的随机报价应用程序,我使用的API将在刷新时生成一个新报价
我在一个 Flutter 应用项目中使用 DART 实现了它,从公共发送的数据被捕获并使用http和provider处理。
我想给应用程序添加一个刷新功能,我在实现它后使用pull_to_refresh不起作用,这里我们应该得到一个新的报价,但报价没有改变,
那么如何克服这个问题,当我进行下拉刷新并且刷新完成后,旧数据被新数据替换
我的主页代码
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;
}
}
}
模型
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,
};
}
主要.dart
import 'package:myquotes/providers/test_provider.dart';
import 'package:myquotes/screens/home_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(
MultiProvider (
providers: [
ChangeNotifierProvider(create: (_) => TestingProvider()),
], child: MyApp(),
)
);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Testing App',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: HomePage(),
);
}
}