在<List>颤振中将未来转换为列表


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是一个返回Future类型变量的函数,我需要将其转换为list以便稍后使用,正如您在注释代码的底部所看到的那样。我尝试使用as List(((类型caster(,但这给了我这个错误:

type‘Future<列表>'不是类型转换中类型"List"的子类型

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

你可以更多地了解FutureBuilder,以及你应该在什么时候成为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}')); 
}
},
)

您可以等待数据准备好:

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

如果您想要一个值来自api调用(Future(的列表。首先,您必须声明一个空列表,并在声明列表中添加list(Future(值。

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

如果您想实现api调用上的列表,请使用futurebuilder和listview.builder

最新更新