我想為我的應用程式使用地圖。我正在使用顫振。我不明白為什么在 onChanged 部分使用 map 時會出錯!這是我使用的代碼我真的不明白為什么它不起作用 mapItem 中的串列來自全域串列,所以我認為這不是問題。而且它不會出錯有什么辦法可以解決嗎?
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:get/get.dart';
import 'package:youtube_ad2/provider/food_name.dart';
import 'result_screen.dart';
import 'package:youtube_ad2/provider/food_provider.dart';
import 'package:provider/provider.dart';
class HomePage extends StatefulWidget {
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
InterstitialAd? interstitialAd;
bool isLoaded = false;
@override
void initState() {
super.initState();
Provider.of<Category>(context, listen: false);
}
@override
void didChangeDependencies() {
//TODO implement didChangeDependencies
super.didChangeDependencies();
InterstitialAd.load(
adUnitId: InterstitialAd.testAdUnitId,
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (ad) {
setState(() {
isLoaded = true;
interstitialAd = ad;
});
print("Ad Loaded");
},
onAdFailedToLoad: (error) {
print("Interstitial Failed to load");
},
),
);
}
Map<String,List> mapItem = {
'??': total,
"??": chn,
"??": kor,
"??": jpn,
"???": asia,
"??": eu,
"????": america,
'?????': aus,
};
String? value;
List<String> listItem = [
"??",
"??",
"??",
"??",
"???",
"??",
"????",
'?????',
];
@override
Widget build(BuildContext context) {
return Container(
// decoration: BoxDecoration(
// image: DecorationImage(
// fit: BoxFit.cover,
// image: AssetImage('assets/images/bg3.png'), // ?? ???
// ),
// ),
child: Scaffold(
backgroundColor: Colors.orange[300],
appBar: AppBar(
automaticallyImplyLeading: false,
elevation: 0,
title: Center(
child: Text(
'??? ??',
style: TextStyle(
fontFamily: 'Chilgok',
color: Colors.yellow.shade300,
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/logo.png',
width: MediaQuery.of(context).size.width / 3,
),
Text('Open!'),
]),
style: ElevatedButton.styleFrom(
side: BorderSide(
width: 5,
color: Colors.red,
),
elevation: 40,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(100)),
primary: Colors.orange[300],
padding: const EdgeInsets.only(
top: 9,
bottom: 15,
left: 20,
right: 20,
),
),
onPressed: () {
if (isLoaded) {
interstitialAd!.show();
Get.to(() => ResultScreen());
} else {
Get.to(() => ResultScreen());
}
},
),
Container(
padding: EdgeInsets.only(
left: 10,
right: 10,
top: 3,
bottom: 5,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.red,
width: 4,
),
),
width: MediaQuery.of(context).size.width / 3,
height: 40,
child: DropdownButton<String>(
underline: DropdownButtonHideUnderline(child: Container()),
dropdownColor: Colors.grey[200],
borderRadius: BorderRadius.circular(10),
elevation: 0,
hint: Text(
"????",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17,
),
),
value: value,
iconSize: 20,
icon: Icon(Icons.arrow_drop_down, color: Colors.red),
isExpanded: true,
items: listItem.map(valueItem).toList(),
onChanged: (value) {setState(() => this.value = value);
context.read<Category>().selectCategory(mapItem(this.value));
},
),
)
],
),
)),
);
}
DropdownMenuItem<String> valueItem(String item) => DropdownMenuItem(
value: item,
child: Text(
item,
style: TextStyle(
color: Colors.black87,
fontSize: 20,
),
),
);
}
為了更容易找到這個,我也會發布一張圖片

uj5u.com熱心網友回復:
您正在訪問value錯誤Map。您必須使用方括號通過提供.square來訪問valuea的。這是一個例子。Mapkey
final Map<String, dynamic> map = {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
'key4': 'value4',
};
print(map['key1']); // Prints value1
對于您的情況
context.read<Category>().selectCategory(mapItem[this.value]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/428998.html
上一篇:R-組合SAME串列中的兩個元素。最好是purrr解決方案
下一篇:合并兩個字典
