我是Flutter的新手,我遇到了一个问题。我需要说明
Facilities facilities = facilitiesList[index];
但是我在单词索引上有一个错误。我正在将列表传递到一个列表视图构建器中。请在下面找到我的代码,这是错误:第一次
hc8w905p1#
请尝试在Map而不是设施点中设置数据,因为facilitiesList是Map列表而不是设施点列表您的代码
正确的方法
Map facilities = facilitiesList[index];
z9gpfhce2#
您有两个不同的facilitiesList。一个在FacilitiesListScreen中,另一个在Facilities中(作为全局变量)。通过在FacilitiesListScreen中声明另一个,另一个超出了作用域,但似乎您希望在某些部分使用它。重命名它可能会解决您的问题,例如重命名为facilitiesListMap。请尝试以下代码。
facilitiesList
FacilitiesListScreen
Facilities
facilitiesListMap
import 'package:flutter/material.dart'; import 'package:oracle_diamond_02/model/facilities.dart'; import 'package:oracle_diamond_02/screen/facilities_details_screen.dart'; class FacilitiesListScreen extends StatefulWidget { FacilitiesListScreen({Key? key}) : super(key: key); _FacilitiesListScreenState createState() => _FacilitiesListScreenState(); } class _FacilitiesListScreenState extends State<FacilitiesListScreen> { final TextStyle dropdownMenuItem = TextStyle(color: Colors.black, fontSize: 18); final primary = Color.fromARGB(255, 206, 84, 84); final secondary = Color(0xfff29a94); final List<Map> facilitiesListMap = [ { "name": "Badminton Court", "location": "UTM Convention Hall", "type": "Indoor Courts", "logoText": "https://firebasestorage.googleapis.com/v0/b/oracle-diamond-02.appspot.com/o/badminton.jpg?alt=media" }, { "name": "PingPong Court", "location": "UTM Convention Hall", "type": "Indoor Courts", "logoText": "https://firebasestorage.googleapis.com/v0/b/oracle-diamond-02.appspot.com/o/pinpong.jpg?alt=media" }, { "name": "Futsal Court", "location": "UTM Convention Hall", "type": "Indoor Courts", "logoText": "https://firebasestorage.googleapis.com/v0/b/oracle-diamond-02.appspot.com/o/futsal.jpg?alt=media" }, { "name": "Tennis Court", "location": "UTM Convention Hall", "type": "Outdoor Courts", "logoText": "https://firebasestorage.googleapis.com/v0/b/oracle-diamond-02.appspot.com/o/tennis.jpg?alt=media" }, ]; @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color(0xfff0f0f0), body: SingleChildScrollView( child: Container( height: MediaQuery.of(context).size.height, width: MediaQuery.of(context).size.width, child: Stack( children: <Widget>[ Container( padding: EdgeInsets.only(top: 145), height: MediaQuery.of(context).size.height, width: double.infinity, child: ListView.builder( itemCount: facilitiesList.length, itemBuilder: (BuildContext context, int index) { Facilities facilities = facilitiesList[index]; Card( child: ListTile( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => FacilitiesDetailsScreen(facilities))); }, )); return buildList(context, index); })), Container( height: 140, width: double.infinity, decoration: BoxDecoration( color: primary, borderRadius: BorderRadius.only( bottomLeft: Radius.circular(30), bottomRight: Radius.circular(30))), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 30), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: <Widget>[ IconButton( onPressed: () {}, icon: Icon( Icons.menu, color: Colors.white, ), ), Text( "List of Facilities", style: TextStyle(color: Colors.white, fontSize: 24), ), IconButton( onPressed: () {}, icon: Icon( Icons.filter_list, color: Colors.white, ), ), ], ), ), ), Container( child: Column( children: <Widget>[ SizedBox( height: 110, ), Padding( padding: EdgeInsets.symmetric(horizontal: 20), child: Material( elevation: 5.0, borderRadius: BorderRadius.all(Radius.circular(30)), child: TextField( // controller: TextEditingController(text: locations[0]), cursorColor: Theme.of(context).primaryColor, style: dropdownMenuItem, decoration: InputDecoration( hintText: "Search Facilities", hintStyle: TextStyle( color: Colors.black38, fontSize: 16), prefixIcon: Material( elevation: 0.0, borderRadius: BorderRadius.all(Radius.circular(30)), child: Icon(Icons.search), ), border: InputBorder.none, contentPadding: EdgeInsets.symmetric( horizontal: 25, vertical: 13)), ), ), ), ], ), ) ], ), ), ), ); } Widget buildList(BuildContext context, int index) { return Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(25), color: Colors.white, ), width: double.infinity, height: 110, margin: EdgeInsets.symmetric(vertical: 10, horizontal: 20), padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Container( width: 50, height: 50, margin: EdgeInsets.only(right: 15), decoration: BoxDecoration( borderRadius: BorderRadius.circular(50), border: Border.all(width: 3, color: secondary), image: DecorationImage( image: NetworkImage(facilitiesListMap[index]['logoText']), fit: BoxFit.fill), ), ), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( facilitiesListMap[index]['name'], style: TextStyle( color: primary, fontWeight: FontWeight.bold, fontSize: 18), ), SizedBox( height: 6, ), Row( children: <Widget>[ Icon( Icons.location_on, color: secondary, size: 20, ), SizedBox( width: 5, ), Text(facilitiesListMap[index]['location'], style: TextStyle( color: primary, fontSize: 13, letterSpacing: .3)), ], ), SizedBox( height: 6, ), Row( children: <Widget>[ Icon( Icons.sports_tennis, color: secondary, size: 20, ), SizedBox( width: 5, ), Text(facilitiesListMap[index]['type'], style: TextStyle( color: primary, fontSize: 13, letterSpacing: .3)), ], ), ], ), ) ], ), ); } }
2条答案
按热度按时间hc8w905p1#
请尝试在Map而不是设施点中设置数据,因为facilitiesList是Map列表而不是设施点列表
您的代码
正确的方法
z9gpfhce2#
您有两个不同的
facilitiesList
。一个在FacilitiesListScreen
中,另一个在Facilities
中(作为全局变量)。通过在FacilitiesListScreen
中声明另一个,另一个超出了作用域,但似乎您希望在某些部分使用它。重命名它可能会解决您的问题,例如重命名为facilitiesListMap
。请尝试以下代码。