代码之家  ›  专栏  ›  技术社区  ›  bunny

flatter:如何停止不必要地一次又一次呈现“Widget build()”?

  •  0
  • bunny  · 技术社区  · 7 年前

    这是弗利特的一个小POC,我的 构建() 函数被一次又一次地调用。

    如果没有任何循环,这是完全不可能的。经过大量研究,我正在打电话 initState()中的“未来” 也

    但是 还 面对 同样的问题 .

    提前谢谢 时间!

    我试过什么。。

    import 'package:flutter/material.dart';
    
    //http_request
    import 'package:http/http.dart' as http; //to handle the http request
    // import 'dart:async'; // for async functions
    import 'dart:async' show Future;
    import 'dart:convert'; //to convert the http response in JSON formate
    
    import 'HomePage.dart';
    
        class Reports extends StatefulWidget {
          @override
          _Reports createState() => _Reports();
        }
    
        class _Reports extends State<Reports> {
      static String url = "Some Url";
      String _response = "abc";
      @override
      void initState() {
        super.initState();
        getTradeName_dropdown_ITR_Computation_DATA();
      }
    
      @override
      Widget build(BuildContext context) {
    
        print('body');
    
        return Scaffold(
          body: Container(
            child: new Text(_response),
          ),
        );
      }
    
      Future getTradeName_dropdown_ITR_Computation_DATA() async {
        try {
          http.Response response =
                  await http.get("http://" + url );
          if (this.mounted) {
            setState(() {
    
              String jsonTradeName_dropdown = response.body;
              _response = jsonTradeName_dropdown;
            });
          }
        } on Exception {
          setState(() {
            _response = "Some error occored. Please Try again...";
          });
        }
      }
    }
    

    输出:

    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    I/flutter ( 5760): body
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Randal Schwartz    7 年前

    理解build()的正确模型是,你应该想象它每秒被调用60次。所以build()例程应该是快速且幂等的。

    实际上,框架进行了一些优化,以便在需要时不调用它,但不应将对build()的过度调用视为失败。

        2
  •  0
  •   CopsOnRoad    7 年前

    你犯了几个错误,下面是正确的代码。你应该使用 String 而不是 Text 显示响应的小部件。

    class _Reports extends State<Reports> {
      static String url = "url";
      String _response = "abc";
      @override
      void initState() {
        super.initState();
        getTradeName_dropdown_ITR_Computation_DATA();
      }
    
      @override
      Widget build(BuildContext context) {
    
        print('body');
    
        return Scaffold(
          body: Container(
            child: new Text(_response),
          ),
        );
      }
    
      Future getTradeName_dropdown_ITR_Computation_DATA() async {
        try {
          http.Response response =
          await http.get("url_goes_here");
    
          if (this.mounted) {
            setState(() {
              String jsonTradeName_dropdown = response.body;
              _response = jsonTradeName_dropdown;
            });
          }
        } on Exception {
          setState(() {
            _response = "Some error occored. Please Try again...";
          });
        }
      }
    }