Flutter how to use Future return value as if variable

后端 未结 5 1014
离开以前
离开以前 2021-02-18 23:50

I want to get Future return value and use it like variable. I have this Future function

  Future _fetchUserInfo(String id)          


        
5条回答
  •  一向
    一向 (楼主)
    2021-02-19 00:32

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    class ThreadExample implements Callable{
    
        @Override
        public String call() throws Exception {
            // TODO Auto-generated method stub
            return "Ashish";
        }
    
    }
    public class FutureThreadExample {
    
        public static void main(String a[]) throws InterruptedException, ExecutionException {
            ExecutorService executorService=Executors.newFixedThreadPool(1);
            List >objList=new ArrayList>();
           for(int i=0;i<10;i++) {
                Future obj=executorService.submit(new ThreadExample());
                objList.add(obj);
           }
            for( Future fut:objList) {
                System.out.println(fut.get());
            }
            executorService.shutdown();
        }
    
    
    }
    

提交回复
热议问题