gpt4 book ai didi

flutter - 通过提供者和 pull_to_refresh 获取新数据

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

我正在制作一个从 API 中获取的随机报价应用程序并返回一个 json,API我使用的将在刷新时生成新报价

我在一个 flutter app 项目中用 DART 实现了它,从公众发送的数据被捕获并用 http 处理。和 provider .

我想为应用程序添加刷新功能,我使用 pull_to_refresh在我实现后它不起作用,在这里我们应该得到一个新的报价,但报价没有改变,

然后如何克服这个问题,当我下拉刷新并且刷新完成后旧数据被新数据替换

My HomePage Code

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 !!!"),
)
],
),
),
]
),
]
),
),
)
);
}

}

My TestingProvider

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;
}
}

}

Model

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,
};
}

Main.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(),
);
}
}

最佳答案

只需检查我所做的更改

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();
}

让我知道它是否有效。

关于flutter - 通过提供者和 pull_to_refresh 获取新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61958122/

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