Conversion of Future<List> to List in Flutter


import 'package:apps/sub_pages/Apparel_brand.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'Brands_api.dart';
import 'Food_brand.dart';
import 'Apparel_brand.dart';
import 'Gift_brand.dart';

class Brands extends StatefulWidget {
  var type;
  Brands({required this.type});

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

class _BrandsState extends State<Brands> {
  late List result;
  void get_data(height, width) async {
    result = await Brands_API.ret_brands(height, width);
  }

  @override
  Widget build(BuildContext context) {
    double? height = MediaQuery.of(context).size.width * 0.40;
    var width = MediaQuery.of(context).size.width * 0.40;
    var interval_width = MediaQuery.of(context).size.width * 0.05;
    // var instance = Brands_API(
    //     height: height, width: width, interval_width: interval_width);
    // Future<List> result = Brands_API.ret_brands(height, width);
    // print(result);
    get_data(height, width);
    print(result);

    // var foods = result[2];
    // var gifts = result[0];
    // var apparel = result[1];
    // late var fin;
    // if (widget.type == 'food') {
    //   fin = Food(food: foods);
    // } else if (widget.type == 'apparel') {
    //   fin = Apparels(apparel: apparel);
    // } else {
    //   fin = Gifts(gifts: gifts);
    // }
    Widget fin = Text('Hello');
    return fin;
  }
}

// class Brand extends StatefulWidget {
//   const Brand({Key? key}) : super(key: key);
//
//   @override
//   _BrandState createState() => _BrandState();
// }
//
// class _BrandState extends State<Brand> {
//   @override
//   Widget build(BuildContext context) {
//     double? height = MediaQuery.of(context).size.width * 0.40;
//     var width = MediaQuery.of(context).size.width * 0.40;
//     var interval_width = MediaQuery.of(context).size.width * 0.05;
//
//     return Scaffold(
//         appBar: AppBar(
//           title: Text(
//             'Brand 1',
//             style: TextStyle(
//               color: Colors.black,
//               fontSize: 40,
//               fontFamily: 'poppins',
//               fontWeight: FontWeight.bold,
//             ),
//           ),
//           backgroundColor: Colors.white,
//           iconTheme: IconThemeData(color: Colors.black),
//           centerTitle: false,
//         ),
//         body: Container(
//           child: Padding(
//             padding: const EdgeInsets.fromLTRB(0, 40, 0, 0),
//             child: Column(
//               mainAxisAlignment: MainAxisAlignment.start,
//               children: [
//                 Row(
//                   mainAxisAlignment: MainAxisAlignment.center,
//                   children: [
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                     Card(height: height, width: width),
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                     Card(height: height, width: width),
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                   ],
//                 ),
//                 SizedBox(height: 15),
//                 Row(
//                   mainAxisAlignment: MainAxisAlignment.center,
//                   children: [
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                     Card(height: height, width: width),
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                     Card(height: height, width: width),
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                   ],
//                 ),
//                 SizedBox(height: 15),
//                 Row(
//                   mainAxisAlignment: MainAxisAlignment.center,
//                   children: [
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                     Card(height: height, width: width),
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                     Card(height: height, width: width),
//                     SizedBox(
//                       height: height,
//                       width: interval_width,
//                     ),
//                   ],
//                 ),
//               ],
//             ),
//           ),
//         ));
//   }
// }
//
// class Card extends StatelessWidget {
//   var width;
//   var height;
//
//   Card({required this.height, required this.width});
//
//   @override
//   Widget build(BuildContext context) {
//     return Container(
//       height: this.height,
//       width: this.width,
//       decoration: BoxDecoration(
//         borderRadius: BorderRadius.circular(20),
//       ),
//       child: Column(
//         children: [
//           Expanded(
//             flex: 3,
//             child: Container(
//               decoration: BoxDecoration(
//                 color: Colors.grey[600],
//               ),
//             ),
//           ),
//           Expanded(
//             flex: 2,
//             child: Container(
//               decoration: BoxDecoration(
//                 color: Colors.grey[300],
//               ),
//             ),
//           )
//         ],
//       ),
//     );
//   }
// }

In this code ret_brands is a function that returns a variable of type Future I need to convert it to list to use it later as you can see in the bottom part of the commented code. I tried using as List() (type caster) but that that gave me this error:

type 'Future<List>' is not a subtype of type 'List' in type cast

---------------Answer---------------

if you want to a list where value are come from api call (Future). first you have to declare a empty list and add list(Future) value in declare list.

 List result= [];
  void get_data(height, width) async {
var list= await Brands_API.ret_brands(height, width);
result.add(list); 
}

if you want to implement list on api call use future builder and listview.builder

---------------Answer---------------

I will encourage you to use FutureBuilder while dealing with future on widget tree.

You can lean more about FutureBuilder and when you should you futureBuilder .

 FutureBuilder(
              future: get_data(x, y),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(child: Text('Please wait its loading...'));
                } else {
                  if (snapshot.hasError)
                    return Center(child: Text('Error: ${snapshot.error}'));
                  else
                    return Center(
                        child: new Text(
                            '${snapshot.data}')); 

                }
              },
            )
---------------Answer---------------

You can just wait until your data prepare :

Future<List> get_data(height, width) async {
  return await Brands_API.ret_brands(height, width);
}

and when use this function do this :

  get_data(height, width).then((List value) => //your code);

or await in async method :

  List result = await get_data(height, width);

Previous : Wordpress: check whether the current post is in any of the specified terms
Next : Highlight matching text filter javascript