Home > other >  future.wait can not return any data
future.wait can not return any data

Time:11-12

class AdView extends StatefulWidget {
  const AdView({Key? key, required String id}) : super(key: key);
  final id = '2';
  @override
  _AdViewState createState() => _AdViewState();
}

class _AdViewState extends State<AdView> {
  final _adService = NewsService();
  late Future<Categories> _futureCategories;
  late Future<AdBanner> _futureBanners;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    getData() async {
      _futureCategories = _adService.getAllCategories();
      _futureBanners = _adService.getAds('2');
      AdBanner foos;
      Categories bars;
      await Future.wait<void>([
        _futureBanners.then((result) => foos = result),
        _futureCategories.then((result) => bars = result),
      ]);
    }

    return FutureBuilder(
        future: getData(),
        builder: (BuildContext context, AsyncSnapshot<dynamic> shot) {
          // (BuildContext context, AsyncSnapshot<List<dynamic>> shot) {
          if (shot.hasData) {
            return ListView.builder(
                itemCount: 2,
                itemBuilder: (BuildContext context, int index) {
                  // return bannerListTile(advertisements, index, context);
                  return const Text('index');
                });
          } else if (shot.hasError) {
            return NewsError(
              errorMessage: '${shot.hasError}',
            );
          } else {
            return const NewsLoading(
              text: 'loading...',
            );
          }
        });
  }
}

I am trying to combine two different API and fetch the results but in this structure I cannot get any data and run only ProgressBarIndicator.

If I am use regular FutureBuilder the JSON calls works without any problem. My goal is get data from two different API's like shot.data[0].value and shot.data[1].value

CodePudding user response:

you made mistake in defining the getData() function.

remove getData from build method and put outside build method because the build is itself a method, you cant define a method inside a method in Dart.

class AdView extends StatefulWidget {
  const AdView({Key? key, required String id}) : super(key: key);
  final id = '2';
  @override
  _AdViewState createState() => _AdViewState();
}

class _AdViewState extends State<AdView> {
  final _adService = NewsService();
  late Future<Categories> _futureCategories;
  late Future<AdBanner> _futureBanners;

  @override
  void initState() {
    super.initState();
  }

  Future getData() async {
    _futureCategories = _adService.getAllCategories();
    _futureBanners = _adService.getAds('2');
    AdBanner foos;
    Categories bars;
    await Future.wait<void>([
      _futureBanners.then((result) => foos = result),
      _futureCategories.then((result) => bars = result),
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: getData(),
        builder: (BuildContext context, AsyncSnapshot<dynamic> shot) {
          // (BuildContext context, AsyncSnapshot<List<dynamic>> shot) {
          if (shot.hasData) {
            return ListView.builder(
                itemCount: 2,
                itemBuilder: (BuildContext context, int index) {
                  // return bannerListTile(advertisements, index, context);
                  return const Text('index');
                });
          } else if (shot.hasError) {
            return NewsError(
              errorMessage: '${shot.hasError}',
            );
          } else {
            return const NewsLoading(
              text: 'loading...',
            );
          }
        });
  }
}
  • Related