Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

How to use a String in a FutureBuilder as Future<String> (Flutter - Dart)

I want to use a String in a FutureBuilder's future value. But if this String is null then I want to use the String from any restful API.

That's why I want to use a String as Future< String > in FutureBuilder.

So, How can I do it, and is it possible?

question from:https://stackoverflow.com/questions/65880308/how-to-use-a-string-in-a-futurebuilder-as-futurestring-flutter-dart

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can skip FutureBuilder and use initState and load the List as shown below :

   List<String> webServiceList ;
  
  List<String> localList = [ 'Foo' ,'Foo' , 'Foo' , 'Foo'] ;

  @override
  void initState() {
    super.initState();
    loadData();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: webServiceList != null ? ListView.builder(
          itemCount:webServiceList.length,
          itemBuilder: (_ , index){
            return Text(webServiceList[index]);
            
          }) : ListView.builder(
          itemCount:localList.length,
          itemBuilder: (_ , index){
            return Text(localList[index]);

          }),

    );
  }

  void loadData() async{

    webServiceList = await ///Get data for web services API ; 

    setState(() {
      
    });
    
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...