実現したいこと
- Conditions must have a static type of 'bool'. Try changing the condition.を解消し、Dart及びFlutterの知見を得て、期待している挙動を得たい。
前提
Flutter × Firebase開発を学習中です。
jsonデータを受け取り、画面に描画させる機能を実装中に以下のメッセージが発生しました。
発生している問題・エラーメッセージ
Conditions must have a static type of 'bool'. Try changing the condition.
該当のソースコード
select_country.dart
1import 'dart:convert'; 2import 'dart:ffi'; 3 4import 'package:flutter/cupertino.dart'; 5import 'package:flutter/services.dart'; 6 7class SelectCountry extends StatefulWidget { 8 const SelectCountry({super.key}); 9 10 @override 11 State<SelectCountry> createState() => _SelectCountryState(); 12} 13 14class _SelectCountryState extends State<SelectCountry> { 15 List<dynamic>? dataRetrieved; 16 List<dynamic>? data; 17 @override 18 void initState() { 19 super.initState(); 20 _getData(); 21 } 22 23 Future _getData() async { 24 final String response = 25 await rootBundle.loadString("assets/CountryCodes.json"); 26 dataRetrieved = await json.decode(response) as List<dynamic>; 27 setState(() { 28 data = dataRetrieved; 29 }); 30 } 31 32 @override 33 Widget build(BuildContext context) { 34 return CupertinoPageScaffold( 35 child: CustomScrollView( 36 slivers: [ 37 const CupertinoSliverNavigationBar( 38 largeTitle: Text("Select Country"), 39 previousPageTitle: "Edit Number", 40 ), 41 SliverList( 42 delegate: SliverChildListDelegate(data ≠ null) ? data!.map((e) => CupertinoListTile(title: Text(e["name"]), trailing: Text(e["dial_code"]),)).toList() : [Center(child: Text("Loading"),)], 43 ), 44 ], 45 ), 46 ); 47 } 48} 49
CountryCodes.json
1[ 2 { 3 "name": "Afghanistan", 4 "dial_code": "+93", 5 "code": "AF" 6 }, 7 { 8 "name": "Aland Islands", 9 "dial_code": "+358", 10 "code": "AX" 11 }, 12 { 13 "name": "Albania", 14 "dial_code": "+355", 15 "code": "AL" 16 }, 17]
試したこと
Conditions must have a static type of 'bool'. Try changing the condition.を和訳したところ、コンディションの静的型は「bool」である必要があります。
コンディションを変更してみてください。という意味だと判明。
Conditions must have a static type of 'bool'. Try changing the condition.でググったところ、以下のページにヒット。
https://stackoverflow.com/questions/63887576/condition-must-have-static-type-of-bool
解決法を見て、それを自分のコードに生かそうと思ったもののasync, await, rootBundleなどの知識がまだまだ浅く、コンディションとは何なのか?という状態のため、何をどのようにするべきなのかわからないという状況です。15分ほど調べてみても、なぜか?どこをどうするべきかというのがわからなかったので有識者の方にご教示していただきたいと考えました。何か追記依頼などがあればコメントにお願いします。
補足情報(FW/ツールのバージョンなど)
[✓] Flutter (Channel stable, 3.3.10, on macOS 13.0.1
22A400 darwin-x64, locale ja-JP)
[✓] Android toolchain - develop for Android devices
(Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.74.3)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
参考にしている動画
https://youtu.be/hjeMe6LSG-c
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。