Flutter/Dart: 无法在屏幕上刷新RefreshIndicator数据

huangapple go评论133阅读模式
英文:

Flutter/Dart: Not able to refresh RefreshIndicator data on screen

问题

我尝试使用RefreshIndicator从数据库中重新加载数据。在超出滚动时,可以从数据库中读取数据,但不知何故无法刷新屏幕上的数据。我是否遗漏或弄错了什么?

  1. Future<void> getMTMavailableamount1() async {
  2. final temp = ModelsPositions().getMasterPositions();
  3. setState() {
  4. _futureList = temp;
  5. }
  6. }
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar: AppBar(
  11. title: const Text('Master Control'),
  12. centerTitle: true,
  13. ),
  14. body: RefreshIndicator(
  15. onRefresh: getMTMavailableamount1,
  16. child: FutureBuilder<List<dynamic>>(
  17. future: _futureList,
  18. builder: (context, snapshot) {
  19. if (snapshot.hasData) {
  20. List<dynamic> positions = snapshot.data ?? [];
  21. return ListView.builder(
  22. itemCount: positions.length,
  23. itemBuilder: (context, index) {
  24. String custID = positions[index][0];
  25. String custName = positions[index][1];
  26. double m2m = double.parse(positions[index][2]);
  27. double available = double.parse(positions[index][3]);
英文:

I'm trying to reload data from DB with RefreshIndicator. Upon over-scroll, Able to read data from DB but somehow not able to refresh the data on the screen. Did i miss/mess up anything?

  1. Future&lt;void&gt; getMTMavailableamount1() async {
  2. final temp = ModelsPositions().getMasterPositions();
  3. setState() {
  4. _futureList = temp;
  5. }
  6. }
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar: AppBar(
  11. title: const Text(&#39;Master Control&#39;),
  12. centerTitle: true,
  13. ),
  14. body: RefreshIndicator(
  15. onRefresh: getMTMavailableamount1,
  16. child: FutureBuilder&lt;List&lt;dynamic&gt;&gt;(
  17. future: _futureList,
  18. builder: (context, snapshot) {
  19. if (snapshot.hasData) {
  20. List&lt;dynamic&gt; positions = snapshot.data ?? [];
  21. return ListView.builder(
  22. itemCount: positions.length,
  23. itemBuilder: (context, index) {
  24. String custID = positions[index][0];
  25. String custName = positions[index][1];
  26. double m2m = double.parse(positions[index][2]);
  27. double available = double.parse(positions[index][3]);

答案1

得分: 1

Introduce new variable called uniqueKey and that should be ValueKey with the value of future.

  1. late ValueKey<Future> uniqueKey;
  2. Future<void> getMTMavailableamount1() async {
  3. final temp = ModelsPositions().getMasterPositions();
  4. setState(() {
  5. _futureList = temp;
  6. uniqueKey = ValueKey(_futureList);
  7. });
  8. }
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. appBar: AppBar(
  13. title: const Text('Master Control'),
  14. centerTitle: true,
  15. ),
  16. body: RefreshIndicator(
  17. onRefresh: getMTMavailableamount1,
  18. child: FutureBuilder<List<dynamic>>(
  19. future: _futureList,
  20. key: uniqueKey,
  21. builder: (context, snapshot) {
  22. if (snapshot.hasData) {
  23. List<dynamic> positions = snapshot.data ?? [];
  24. return ListView.builder(
  25. itemCount: positions.length,
  26. itemBuilder: (context, index) {
  27. String custID = positions[index][0];
  28. String custName = positions[index][1];
  29. double m2m = double.parse(positions[index][2]);
  30. double available = double.parse(positions[index][3]);

Make sure you initiated uniqueKey at initState().

  1. @override
  2. void initState() {
  3. super.initState();
  4. _futureList = ModelsPositions().getMasterPositions();
  5. uniqueKey = ValueKey<Future>(_futureList);
  6. }

Edited

You misused setState. your code creates a new inline function instead of calling setState of State. setState looks like as following.

  1. setState(() {
  2. _futureList = temp;
  3. uniqueKey = ValueKey(_futureList);
  4. });
英文:

Introduce new variable called uniqueKey and that should be ValueKey with the value of future.

  1. late ValueKey&lt;Future&gt; uniqueKey;
  2. Future&lt;void&gt; getMTMavailableamount1() async {
  3. final temp = ModelsPositions().getMasterPositions();
  4. setState(() {
  5. _futureList = temp;
  6. uniqueKey = ValueKey(_futureList);
  7. });
  8. }
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. appBar: AppBar(
  13. title: const Text(&#39;Master Control&#39;),
  14. centerTitle: true,
  15. ),
  16. body: RefreshIndicator(
  17. onRefresh: getMTMavailableamount1,
  18. child: FutureBuilder&lt;List&lt;dynamic&gt;&gt;(
  19. future: _futureList,
  20. key: uniqueKey,
  21. builder: (context, snapshot) {
  22. if (snapshot.hasData) {
  23. List&lt;dynamic&gt; positions = snapshot.data ?? [];
  24. return ListView.builder(
  25. itemCount: positions.length,
  26. itemBuilder: (context, index) {
  27. String custID = positions[index][0];
  28. String custName = positions[index][1];
  29. double m2m = double.parse(positions[index][2]);
  30. double available = double.parse(positions[index][3]);

Make sure you initiated uniqueKey at initState().

  1. @override
  2. void initState() {
  3. super.initState();
  4. _futureList = ModelsPositions().getMasterPositions();
  5. uniqueKey = ValueKey&lt;Future&gt;(_futureList);
  6. }

Edited

You misused setState. your code creates a new inline function instead of calling setState of State. setState looks like as following.

  1. setState(() {
  2. _futureList = temp;
  3. uniqueKey = ValueKey(_futureList);
  4. });

答案2

得分: 0

  1. import 'dart:async';
  2. import 'package:e2/pages/authorize.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:fluttertoast/fluttertoast.dart';
  5. import 'package:e2/Models/model_positions.dart';
  6. import 'package:http/http.dart' as http;
  7. import 'dart:convert';
  8. class MasterControl extends StatefulWidget {
  9. const MasterControl({super.key});
  10. @override
  11. State<MasterControl> createState() => _MasterControlState();
  12. }
  13. class _MasterControlState extends State<MasterControl> {
  14. List<dynamic> _selectedItems = [];
  15. // List<MasterPositions> positions = [];
  16. late Future<List<dynamic>> _futureList;
  17. @override
  18. void initState() {
  19. super.initState();
  20. _futureList = ModelsPositions().getMasterPositions();
  21. uniqueKey = ValueKey<Future>(_futureList);
  22. }
  23. late ValueKey<Future> uniqueKey;
  24. Future<void> getMTMavailableamount1() async {
  25. final response = await http.post(
  26. Uri.parse('https://jhkmmy7zhx2dj2yag74j7p5k2e0fyqnj.lambda-url.ap-south-1.on.aws/'),
  27. headers: <String, String>{
  28. 'Content-Type': 'application/json; charset=UTF-8',
  29. },
  30. );
  31. final temp = ModelsPositions().getMasterPositions();
  32. setState() {
  33. _futureList = temp;
  34. uniqueKey = ValueKey(_futureList);
  35. }
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return Scaffold(
  40. appBar: AppBar(
  41. title: const Text('Master Control'),
  42. centerTitle: true,
  43. ),
  44. body: RefreshIndicator(
  45. onRefresh: getMTMavailableamount1,
  46. child: FutureBuilder<List<dynamic>>(
  47. future: _futureList,
  48. key: uniqueKey,
  49. builder: (context, snapshot) {
  50. if (snapshot.hasData) {
  51. List<dynamic> positions = snapshot.data ?? [];
  52. return ListView.builder(
  53. itemCount: positions.length,
  54. itemBuilder: (context, index) {
  55. String custID = positions[index][0];
  56. String custName = positions[index][1];
  57. double m2m = double.parse(positions[index][2]);
  58. double available = double.parse(positions[index][3]);
  59. // String symbol = positions[index][4];
  60. return Card(
  61. child: Row(children: [
  62. Checkbox(
  63. value: _selectedItems.contains(positions[index]),
  64. onChanged: (value) {
  65. setState(() {
  66. if (value == null) {
  67. return;
  68. }
  69. if (value) {
  70. _selectedItems.add(positions[index]);
  71. } else {
  72. _selectedItems.removeWhere((item) => item == positions[index]);
  73. }
  74. });
  75. },
  76. ),
  77. Flexible(
  78. child: Column(
  79. crossAxisAlignment: CrossAxisAlignment.stretch,
  80. children: [
  81. const SizedBox(height: 2),
  82. Text(custID),
  83. const SizedBox(height: 2),
  84. Text(custName),
  85. const SizedBox(height: 2),
  86. ],
  87. ),
  88. ),
  89. Flexible(
  90. // fit: FlexFit.tight,
  91. child: Column(
  92. mainAxisAlignment: MainAxisAlignment.center,
  93. crossAxisAlignment: CrossAxisAlignment.stretch,
  94. children: [
  95. const SizedBox(height: 3),
  96. Text(
  97. 'MTM : $m2m',
  98. softWrap: false,
  99. style: TextStyle(
  100. fontFamily: 'Roboto',
  101. color: m2m > 0.0
  102. ? const Color.fromARGB(255, 11, 180, 16)
  103. : Colors.red[600],
  104. ),
  105. ),
  106. const SizedBox(height: 5),
  107. Text(
  108. 'Available : $available',
  109. softWrap: false,
  110. style: TextStyle(
  111. fontFamily: 'Roboto',
  112. color: available > 0.0
  113. ? const Color.fromARGB(255, 11, 180, 16)
  114. : Colors.red[600],
  115. ),
  116. ),
  117. const SizedBox(height: 5),
  118. ],
  119. ),
  120. ),
  121. ]),
  122. );
  123. },
  124. );
  125. } else if (snapshot.hasError) {
  126. return const Center(child: Text('Failed to fetch Positions Summary'));
  127. }
  128. return const Center(child: CircularProgressIndicator());
  129. },
  130. ),
  131. ),
  132. );
  133. }
  134. }
  135. class LambdaResponse {
  136. late int statusCode;
  137. late String msg;
  138. LambdaResponse({required this.statusCode, required this.msg});
  139. factory LambdaResponse.fromJson(String response) {
  140. print('here1');
  141. print(response);
  142. return LambdaResponse(
  143. statusCode: 1,
  144. msg: response,
  145. );
  146. }
  147. }

这是您提供的Dart代码的翻译版本。如您所要求,我已经去掉了代码部分的翻译。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。

英文:
  1. import &#39;dart:async&#39;;
  2. import &#39;package:e2/pages/authorize.dart&#39;;
  3. import &#39;package:flutter/material.dart&#39;;
  4. import &#39;package:fluttertoast/fluttertoast.dart&#39;;
  5. import &#39;package:e2/Models/model_positions.dart&#39;;
  6. import &#39;package:http/http.dart&#39; as http;
  7. import &#39;dart:convert&#39;;
  8. class MasterControl extends StatefulWidget {
  9. const MasterControl({super.key});
  10. @override
  11. State&lt;MasterControl&gt; createState() =&gt; _MasterControlState();
  12. }
  13. class _MasterControlState extends State&lt;MasterControl&gt; {
  14. List&lt;dynamic&gt; _selectedItems = [];
  15. //List&lt;MasterPositions&gt; positions = [];
  16. late Future&lt;List&lt;dynamic&gt;&gt; _futureList;
  17. @override
  18. void initState() {
  19. super.initState();
  20. _futureList = ModelsPositions().getMasterPositions();
  21. uniqueKey = ValueKey&lt;Future&gt;(_futureList);
  22. }
  23. late ValueKey&lt;Future&gt; uniqueKey;
  24. Future&lt;void&gt; getMTMavailableamount1() async {
  25. final response = await http.post(
  26. Uri.parse(
  27. &#39;https://jhkmmy7zhx2dj2yag74j7p5k2e0fyqnj.lambda-url.ap-south-1.on.aws/&#39;),
  28. headers: &lt;String, String&gt;{
  29. &#39;Content-Type&#39;: &#39;application/json; charset=UTF-8&#39;,
  30. },
  31. );
  32. final temp = ModelsPositions().getMasterPositions();
  33. setState() {
  34. _futureList = temp;
  35. uniqueKey = ValueKey(_futureList);
  36. }
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return Scaffold(
  41. appBar: AppBar(
  42. title: const Text(&#39;Master Control&#39;),
  43. centerTitle: true,
  44. ),
  45. body: RefreshIndicator(
  46. onRefresh: getMTMavailableamount1,
  47. child: FutureBuilder&lt;List&lt;dynamic&gt;&gt;(
  48. future: _futureList,
  49. key: uniqueKey,
  50. builder: (context, snapshot) {
  51. if (snapshot.hasData) {
  52. List&lt;dynamic&gt; positions = snapshot.data ?? [];
  53. return ListView.builder(
  54. itemCount: positions.length,
  55. itemBuilder: (context, index) {
  56. String custID = positions[index][0];
  57. String custName = positions[index][1];
  58. double m2m = double.parse(positions[index][2]);
  59. double available = double.parse(positions[index][3]);
  60. //String symbol = positions[index][4];
  61. return Card(
  62. child: Row(children: [
  63. Checkbox(
  64. value: _selectedItems.contains(positions[index]),
  65. onChanged: (value) {
  66. setState(() {
  67. if (value == null) {
  68. return;
  69. }
  70. if (value) {
  71. _selectedItems.add(positions[index]);
  72. } else {
  73. _selectedItems.removeWhere(
  74. (item) =&gt; item == positions[index]);
  75. }
  76. });
  77. },
  78. ),
  79. Flexible(
  80. child: Column(
  81. crossAxisAlignment: CrossAxisAlignment.stretch,
  82. children: [
  83. const SizedBox(height: 2),
  84. Text(custID),
  85. const SizedBox(height: 2),
  86. Text(custName),
  87. const SizedBox(height: 2),
  88. ],
  89. ),
  90. ),
  91. Flexible(
  92. //fit: FlexFit.tight,
  93. child: Column(
  94. mainAxisAlignment: MainAxisAlignment.center,
  95. crossAxisAlignment: CrossAxisAlignment.stretch,
  96. children: [
  97. const SizedBox(height: 3),
  98. Text(
  99. &#39;MTM : $m2m&#39;,
  100. softWrap: false,
  101. style: TextStyle(
  102. fontFamily: &#39;Roboto&#39;,
  103. color: m2m &gt; 0.0
  104. ? const Color.fromARGB(255, 11, 180, 16)
  105. : Colors.red[600]),
  106. ),
  107. const SizedBox(height: 5),
  108. Text(
  109. &#39;Available : $available&#39;,
  110. softWrap: false,
  111. style: TextStyle(
  112. fontFamily: &#39;Roboto&#39;,
  113. color: available &gt; 0.0
  114. ? const Color.fromARGB(255, 11, 180, 16)
  115. : Colors.red[600]),
  116. ),
  117. const SizedBox(height: 5),
  118. ],
  119. ),
  120. ),
  121. ]));
  122. },
  123. );
  124. } else if (snapshot.hasError) {
  125. return const Center(
  126. child: Text(&#39;Failed to fetch Positions Summary&#39;));
  127. }
  128. return const Center(child: CircularProgressIndicator());
  129. },
  130. ),
  131. ),);
  132. }
  133. }
  134. class LambdaResponse {
  135. late int statusCode;
  136. late String msg;
  137. LambdaResponse({required this.statusCode, required this.msg});
  138. factory LambdaResponse.fromJson(String response) {
  139. print(&#39;here1&#39;);
  140. print(response);
  141. return LambdaResponse(
  142. statusCode: 1,
  143. msg: response,
  144. );
  145. }
  146. }

huangapple
  • 本文由 发表于 2023年3月9日 18:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定