【Flutter问题记录】001-查询数据持续加载的问题和解决

 

一. 持续加载

1.问题描述

在使用DIO获取服务器的数据后,页面显示正常,但是后台持续请求服务器的数据,正常应该只请求一次,但是现在实际中是持续请求服务器,问:为什么会持续加载 日志如下:

Initializing hot reload...
Syncing files to device OPPO R17...
Reloaded 4 of 554 libraries in 499ms.
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求
I/flutter ( 4306): {code: SUCCESS, params: null, message: null, data: [{problemID: 017f67fdf2ec465f8e1468582dc5601c, userID: null, proConent: 哈哈哈, problemDes: 暂无答案}, {problemID: 06ba5cacdec44a33aa1275f043f91beb, userID: null, proConent: 88, problemDes: 暂无答案}, {problemID: 12b35b6678c94d7da0a7c5b55e025d37, userID: null, proConent: 11对战平台, problemDes: 暂无答案}, {problemID: 25e85364ce5f41f78b5c19e49a08675b, userID: null, proConent: 为什么SQL报错, problemDes: 暂无答案}, {problemID: 26759f45a00440bcabb839cf14793243, userID: null, proConent: 1, problemDes: null}, {problemID: 766d543199344557829df76ad4dc5962, userID: null, proConent: 恩恩恩, problemDes: 暂无答案}, {problemID: 90d31efcdd7a48c889e911b5badce545, userID: null, proConent: 2, problemDes: null}, {problemID: ca69b0613cad480b9d0b33394c0f5c65, userID: null, proConent: 测试, problemDes: 暂无答案}]}
I/flutter ( 4306): [Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData', Instance of 'ProListData']
I/flutter ( 4306): 请求

2.页面效果

【Flutter问题记录】001-查询数据持续加载的问题和解决_第1张图片

3. 代码

 

import 'package:dio/dio.dart';
import 'package:essay/component/Mystaticval.dart';
import 'package:essay/dto/ProListEntity.dart';
import 'package:essay/pages/my_page/DetailedQuestions.dart';
import 'package:flutter/material.dart';

class AllProblem extends StatefulWidget {
  AllProblem({Key key}) : super(key: key);

  _AllProblem createState() => _AllProblem();
}

class _AllProblem extends State {
  List result = List();

  @override
  Widget build(BuildContext content) {
    queryProblemByNet('001');
    return Scaffold(
      appBar: AppBar(
        title: Text("问题编辑"),
      ),
      body: ListView.builder(
          itemCount: result.length,
          itemBuilder: (content, index) {
            return ListTile(
              title: new Text('${result[index].proConent}'),
              subtitle: new Text('${result[index].problemDes}'),
              trailing: Icon(Icons.arrow_right),
              onTap: () {
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => DetailedQuestions()));
              },
            );
          }),
    );
  }

  void queryProblemByNet(String userID) async {
    Map parameters = {"userID": "$userID"};
    var response = await Dio()
        .post("$essayUrl/study/query/problem", data: parameters) as Response;
    var res = response.data;
    print("请求");
    print(res.toString());
    ProListEntity problemDTO = ProListEntity.fromJson(res);

    setState(() {
      this.result = problemDTO.data;
    });
    print(result.toString());
  }
}

4. 问题解决

数据请求放在初始化时完成

 @override
  void initState() {
    super.initState();
    queryProblemByNet("001");
  }

把原本放在buid中的请求

 queryProblemByNet("001");

放到初始化即可解决这个问题

你可能感兴趣的:(Flutter)