Flutter 中 Future<List> 到 List 的转换

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],
//               ),
//             ),
//           )
//         ],
//       ),
//     );
//   }
// }

在这段代码中,ret_brands 是一个函数,returns 一个 Future 类型的变量,我需要将其转换为列表以便稍后使用,正如您在注释代码底部看到的那样. 我尝试使用 as List() (类型施法者),但这给了我这个错误:

type 'Future<List>' 不是类型 cast

中类型 'List' 的子类型

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

我会鼓励您在处理小部件树的未来时使用 FutureBuilder

您可以进一步了解 FutureBuilder and

 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}')); 

                }
              },
            )

您可以等到数据准备好:

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

并且在使用此功能时执行此操作:

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

或在异步方法中等待:

  List result = await get_data(height, width);