【问题标题】:Flutter - setState isn't working inside http response codeFlutter - setState 在 http 响应代码中不起作用
【发布时间】:2019-07-05 05:28:33
【问题描述】:

我有一个简单的页面来从服务器获取数据并解析响应。 我有两个用户可见的小部件,一个是 _loadingWidget() 小部件,另一个是 errorWidget() 小部件。

我添加了一些打印语句来显示代码正在执行的位置。一切正常,但 setState for errorWidget() 不工作。 我在这里做错了什么?

import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:validators/validators.dart';

import '../strings.dart';

class FetchDataPage extends StatefulWidget {
  final String text;
  FetchDataPage({Key key, @required this.text}) : super(key: key);

  @override
  _FetchDataState createState() => _FetchDataState();
}

class _FetchDataState extends State<FetchDataPage> {
  String _productUrl;
  Widget pageWidget;

  @override
  Widget build(BuildContext context) {
    pageWidget = _loadingWidget();
    _productUrl = widget.text;
    checkPrice(_productUrl);
    return Scaffold(
      body: Container(padding: EdgeInsets.all(20.0), child: pageWidget),
    );
  }

  Widget _loadingWidget() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Center(
          child: CircularProgressIndicator(
            backgroundColor: Colors.transparent,
            valueColor: AlwaysStoppedAnimation<Color>(Colors.blueGrey),
          ),
        ),
        SizedBox(height: 20.0),
        Text(
          "Checking Price...",
          style: TextStyle(
            color: Colors.blueGrey,
            fontSize: 20.0,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  Widget errorWidget() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Center(
          child: Icon(
            Icons.sentiment_dissatisfied,
            color: Colors.red,
            size: 60.0,
          ),
        ),
        SizedBox(height: 20.0),
        Center(
          child: Text(
            "Product not found in database.\nPlease try again later.",
            textAlign: TextAlign.center,
            style: TextStyle(
              color: Colors.blueGrey,
              fontSize: 17.0,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ],
    );
  }

  void checkPrice(String productUrl) {
    bool isUrl = isURL(productUrl, requireProtocol: true, requireTld: true);
    if (!isUrl) {
      RegExp regExp = new RegExp(r"((\w+:\/\/\S+)|(\w+[\.:]\w+\S+))[^\s,\.]");
      setState(() {
        productUrl = regExp.stringMatch(productUrl).toString();
      });
    }
    /*
    //setState is Working fine here!
    setState(() {
      pageWidget = errorWidget();
    });
    */
    var response = getLatestPrice(productUrl);
    response.then((response) {
      /*
      //It's Not Working Here :(
      setState(() {
        pageWidget = errorWidget();
      });
      */

      print("Got Response: " + response.body.toString());
      if (response.statusCode == 200) {
        var loginData = json.decode(response.body);
        bool status = loginData["status"];
        print("STATUS: " + status.toString());
        if (status) {
          //This print statement is also working fine!
          print(loginData["productUrl"]);
        } else {
          //This isn't working either
          setState(() {
            pageWidget = errorWidget();
          });
        }
      } else {
        //Not Working
        setState(() {
          pageWidget = errorWidget();
        });
      }
    });
  }
}

Future<http.Response> getLatestPrice(productUrl) async {
  var url = Strings.baseUrl + 'api/checkPrice';
  var response = await http.post(url, body: {
    'product_url': productUrl,
  }, headers: {
    HttpHeaders.authorizationHeader:
        "Basic " + base64Encode(utf8.encode('username:password'))
  });
  return response;
}

【问题讨论】:

  • "but setState for errorWidget() is not working." - 定义“不工作”
  • 我注意到的第一个问题,删除setState表单构建方法。如果有,请分享错误代码。
  • @pskink 无法正常工作,因为它没有重建小部件以显示 errorWidget()
  • @mathronaut 感谢您的建议,我从构建方法中删除了 setstate。我没有收到任何错误,它只是忽略了setState
  • @pskink 是的,我可以在日志中看到其余的打印语句。

标签: flutter dart


【解决方案1】:

我通过设置 bool 来修复它:

import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:validators/validators.dart';

import '../strings.dart';

class FetchDataPage extends StatefulWidget {
  final String text;
  FetchDataPage({Key key, @required this.text}) : super(key: key);

  @override
  _FetchDataState createState() => _FetchDataState();
}

class _FetchDataState extends State<FetchDataPage> {
  String _productUrl;
  bool showError = false;

  @override
  Widget build(BuildContext context) {
    _productUrl = widget.text;
    if (_productUrl != null) {
      checkPrice(_productUrl);
    }
    return Scaffold(
      body: Container(padding: EdgeInsets.all(20.0), child: pageWidget()),
    );
  }

  Widget pageWidget() {
    if (showError == false) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.transparent,
              valueColor: AlwaysStoppedAnimation<Color>(Colors.blueGrey),
            ),
          ),
          SizedBox(height: 20.0),
          Text(
            "Checking Price...",
            style: TextStyle(
              color: Colors.blueGrey,
              fontSize: 20.0,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      );
    } else {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Center(
            child: Icon(
              Icons.sentiment_dissatisfied,
              color: Colors.red,
              size: 60.0,
            ),
          ),
          SizedBox(height: 20.0),
          Center(
            child: Text(
              "Product not found in database.\nPlease try again later.",
              textAlign: TextAlign.center,
              style: TextStyle(
                color: Colors.blueGrey,
                fontSize: 17.0,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      );
    }
  }

  void checkPrice(String productUrl) {
    bool isUrl = isURL(productUrl, requireProtocol: true, requireTld: true);
    if (!isUrl) {
      RegExp regExp = new RegExp(r"((\w+:\/\/\S+)|(\w+[\.:]\w+\S+))[^\s,\.]");
      setState(() {
        productUrl = regExp.stringMatch(productUrl).toString();
      });
    }
    var response = getLatestPrice(productUrl);
    response.then((response) {
      if (response.statusCode == 200) {
        var loginData = json.decode(response.body);
        bool status = loginData["status"];
        if (status) {
          print(loginData["productUrl"]);
        } else {
          setState(() {
            showError = true;
          });
        }
      } else {
        setState(() {
          showError = true;
        });
      }
    });
  }
}

Future<http.Response> getLatestPrice(productUrl) async {
  var url = Strings.baseUrl + 'api/checkPrice';
  var response = await http.post(url, body: {
    'product_url': productUrl,
  }, headers: {
    HttpHeaders.authorizationHeader:
        "Basic " + base64Encode(utf8.encode('username:password'))
  });
  return response;
}

【讨论】:

  • 嗯,也许我错了,但调用异步方法的方式也可能是错误的。应在后面添加 await 以等待获取响应。 var response = await getLatestPrice(productUrl); if(response.statusCode == 200)....
  • 此解决方案仅在检查响应代码后运行一行,提供我不知道问题出在哪里
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 2022-10-01
  • 2020-11-12
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 2016-05-13
相关资源
最近更新 更多