【问题标题】:Dealing with Future<String> Inside of a Widget处理小部件内部的 Future<String>
【发布时间】:2021-02-19 17:59:02
【问题描述】:

我正在尝试让我的应用程序将 HTTP 请求的结果显示为按钮。这是发出请求的函数。

Future<String> client() async {
  return await HttpRequest.getString('localhost:8000');
}

这是我想在其中显示它的按钮。

ElevatedButton(
  onPressed: () {},
  child: Text('This is a Deck'),
),

我想显示请求来代替字符串“This is a Deck”。我试过await client()client().toString() 但这些都不起作用。如果有帮助,这是我的完整小部件类。

class DeckButtons extends StatelessWidget {
  DeckButtons({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          ElevatedButton(
            onPressed: () {},
            child: Text('This is a Deck'),
          ),
          IconButton(
            icon: Icon(Icons.add),
            onPressed: () {},
            tooltip: 'Add a Card',
          ),
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {},
            tooltip: 'See All Cards',
          ),
          IconButton(
            icon: Icon(Icons.delete),
            onPressed: () {},
            tooltip: 'Delete this Deck',
          )
        ],
      ),
    );
  }
}

我是 Flutter 和异步编程的新手,如果这个问题的答案很明显,我深表歉意。

编辑:有人告诉我我的问题与"What is a Future and how do I use it?" 重复。我不认为我的问题是重复的,因为尽管答案的某些部分可能适用于我的情况,但问题和接受的答案过于宽泛,涵盖了很多附加信息。此外,我仍然没有解决我的问题,我不确定该帖子中给出的答案是否对我有任何帮助

【问题讨论】:

标签: flutter asynchronous dart


【解决方案1】:

对于执行异步代码,有很多选择,但下面的一个将是完成工作的最简单的选择。

示例:

String _deckString  = "This is a Deck"; /// This variable should be used in a text widget, so until the network fetch the real data, till then default text can be shown

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    client().then((value) { /// This code will run only when async operation done inside method client(), so value is its response which need to be shown
      setState(() {
        _deckString = value;
      });
    });
  }

完整代码:

class _HomeScreenState extends State<HomeScreen> {
  String _deckString  = "This is a Deck";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    client().then((value) {
      setState(() {
        _deckString = value;
      });
    });
  }

  Future<String> client() async {
     return await HttpRequest.getString('localhost:8000');
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Example'),),
      body: Center(
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {},
              child: Text(_deckString),
            ),
            IconButton(
              icon: Icon(Icons.add),
              onPressed: () {},
              tooltip: 'Add a Card',
            ),
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {},
              tooltip: 'See All Cards',
            ),
            IconButton(
              icon: Icon(Icons.delete),
              onPressed: () {},
              tooltip: 'Delete this Deck',
            )
          ],
        ),
      ),
    );
  }
}

【讨论】:

  • 我还建议使用 try catch、async 和 await,否则您将体验回调地狱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
相关资源
最近更新 更多