質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Q&A

解決済

1回答

2041閲覧

flutter ListViewのエラーについて (Null check operator used on a null value)

OSARU_2020

総合スコア15

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

0グッド

0クリップ

投稿2022/08/12 05:49

解決したいこと

FutureBuilderの中でListViewを使って、リストを表示したいが上手くいきません。

発生しているエラー

表示されるはずのリストが表示されず、画面が真っ白になってしまいます。
エラーメッセージの上部を見ていると、ListViewのwidgetが悪さをしてそうな印象です。

↓エラーメッセージ全文

======== Exception caught by widgets library ======================================================= The following _CastError was thrown building NotificationListener<KeepAliveNotification>: Null check operator used on a null value The relevant error-causing widget was: ListView ListView:file:///Users/satoruyamano/AndroidStudioProjects/funandpractise/lib/ui/bbs/feed_bbs.dart:102:41 When the exception was thrown, this was the stack: ==================================================================================================== 文字数がオーバーするため一部省略

該当のソースコード

↓悪さをしてそうな箇所を抜粋したコード(この下に全コードも記載します)

Dart

1FutureBuilder( 2 future: futureData, 3 builder: ((context, snapshot) { 4 if (snapshot.hasError) { 5 return Center( 6 child: Text( 7 snapshot.error.toString(), 8 style: const TextStyle(color: Colors.black), 9 )); 10 } else if (snapshot.hasData) { 11 return ListView.builder( 12 itemCount: 13 (snapshot.data as List<BBSPost>).length, 14 itemBuilder: ((context, index) { 15 BBSPost post = 16 (snapshot.data as List<BBSPost>)[index]; 17 18 return BBSFeedWidget(post: [post]); 19 }), 20 ); 21 } 22 return Center( 23 child: CircularProgressIndicator( 24 valueColor: AlwaysStoppedAnimation( 25 Theme.of(context).primaryColor))); 26 }), 27 ), 28

↓全文

Dart

1 2class BBSFeedPage extends StatefulWidget { 3 const BBSFeedPage({Key? key}) : super(key: key); 4 5 6 State<BBSFeedPage> createState() => _BBSFeedPage(); 7} 8 9class _BBSFeedPage extends State<BBSFeedPage> { 10 late Future<List<BBSPost>> futureData; 11 12 Future<List<BBSPost>> readData() async { 13 PostgrestResponse<dynamic> res = await Supabase.instance.client 14 .from('bbs_view') 15 .select() 16 .order('created_at', ascending: false) 17 .execute(); 18 return (res.data as List<dynamic>).map((e) => BBSPost.fromMap(e)).toList(); 19 } 20 21 22 void initState() { 23 super.initState(); 24 futureData = readData(); 25 } 26 27 28 Widget build(BuildContext context) { 29 return Scaffold( 30 backgroundColor: Theme.of(context).bottomAppBarColor, 31 key: _scaffoldKey, 32 body: SafeArea( 33 child: DefaultTabController( 34 length: 2, 35 child: NestedScrollView( 36 headerSliverBuilder: 37 (BuildContext context, bool innerBoxIsScrolled) { 38 return <Widget>[ 39 //TODO:ーーーーーーーーーーーーーーーーーーーーーーーーーーーーSliverAppBar 40 SliverAppBar( 41 backgroundColor: Theme.of(context).bottomAppBarColor, 42 floating: true, 43 pinned: false, 44 snap: false, 45 leading: IconButton( 46 icon: const Icon(Icons.menu), 47 color: Theme.of(context).iconTheme.color, 48 onPressed: () async { 49 _scaffoldKey.currentState?.openDrawer(); 50 }), 51 forceElevated: true, 52 expandedHeight: 50, 53 automaticallyImplyLeading: false, 54 ), 55 _tabSection(), 56 ]; 57 }, 58 //TODO:ーーーーーーーーーーーーーーーーーーーーーーーーーーーーBody 59 body: TabBarView( 60 children: [ 61 FutureBuilder( 62 future: futureData, 63 builder: ((context, snapshot) { 64 if (snapshot.hasError) { 65 return Center( 66 child: Text( 67 snapshot.error.toString(), 68 style: const TextStyle(color: Colors.black), 69 )); 70 } else if (snapshot.hasData) { 71 return ListView.builder( 72 itemCount: 73 (snapshot.data as List<BBSPost>).length, 74 itemBuilder: ((context, index) { 75 BBSPost post = 76 (snapshot.data as List<BBSPost>)[index]; 77 78 return BBSFeedWidget(post: [post]); 79 }), 80 ); 81 } 82 return Center( 83 child: CircularProgressIndicator( 84 valueColor: AlwaysStoppedAnimation( 85 Theme.of(context).primaryColor))); 86 }), 87 ), 88 ], 89 ), 90 ), 91 ), 92 ), 93 //TODO:ーーーーーーーーーーーーーーーーーーーーーーーーーーーー親スレッド作成ボタン 94 floatingActionButton: FloatingActionButton( 95 backgroundColor: Theme.of(context).primaryColor, 96 onPressed: () { 97 Navigator.push( 98 context, 99 MaterialPageRoute( 100 builder: (context) => const AddBBSPage(), 101 fullscreenDialog: true, 102 //fullscreenDialog: true, 103 ), 104 ); 105 }, 106 child: 107 Icon(FeatherIcons.plus, color: Theme.of(context).bottomAppBarColor), 108 ), 109 ); 110 } 111} 112 113//TODO:ーーーーーーーーーーーーーーーーーーーーーーーーーーーーTabBar 114Widget _tabSection() { 115 return const SliverPersistentHeader( 116 pinned: true, 117 delegate: _StickyTabBarDelegate( 118 tabBar: TabBar( 119 labelColor: Colors.black, 120 tabs: [ 121 Tab( 122 text: 'フォロー', 123 ), 124 Tab( 125 text: '新着スレッド', 126 ) 127 ], 128 ), 129 ), 130 ); 131} 132 133//TODO:ーーーーーーーーーーーーーーーーーーーーーーーーーーーーSliverPersistentHeaderDelegateを継承したTabBarを作る 134class _StickyTabBarDelegate extends SliverPersistentHeaderDelegate { 135 const _StickyTabBarDelegate({required this.tabBar}); 136 137 final TabBar tabBar; 138 139 140 double get minExtent => tabBar.preferredSize.height; 141 142 143 double get maxExtent => tabBar.preferredSize.height; 144 145 146 Widget build( 147 BuildContext context, 148 double shrinkOffset, 149 bool overlapsContent, 150 ) { 151 return Material( 152 elevation: 1, 153 child: Container( 154 color: Theme.of(context).bottomAppBarColor, 155 child: tabBar, 156 ), 157 ); 158 } 159 160 161 bool shouldRebuild(_StickyTabBarDelegate oldDelegate) { 162 return tabBar != oldDelegate.tabBar; 163 } 164} 165 166 167 168class BBSPost { 169 BBSPost({ 170 required this.postID, 171 required this.title, 172 required this.text, 173 required this.purpose, 174 required this.categoryID, 175 required this.idol, 176 required this.createdAt, 177 required this.username, 178 required this.avatar_url, 179 required this.likednum, 180 required this.avatar_initial, 181 //required this.commentnum, 182 }); 183 184 final int postID; 185 final String title; 186 final String text; 187 final String purpose; 188 final int categoryID; 189 final String idol; 190 final DateTime createdAt; 191 final String? username; 192 final String? avatar_url; 193 final int? likednum; 194 final String avatar_initial; 195 196 factory BBSPost.fromMap(Map<String, dynamic> map) { 197 return BBSPost( 198 postID: map['post_id'], 199 title: map['title'], 200 text: map['text'], 201 purpose: map['purpose_name'], 202 categoryID: map['category_id'], 203 idol: map['idol_name'], 204 createdAt: DateTime.parse(map['created_at']), 205 username: map['username'], 206 avatar_url: map['avatar_url'], 207 likednum: map['likednum'], 208 avatar_initial: map['avatar_initial'], 209 ); 210 } 211} 212 213

試したこと

  • flutter upgrade を試してみる

※原因を検索して色々試したものの、クリティカルな対処法を発見できず、上記以外で特に記載できるようなことは試せていません。

補足情報

AndroidStudioでコーディングしています。
flutterのバージョンは最新です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Null check operator used on a null value

上記のエラーメッセージは例えば

String? title; int length = title!.length;

のようにNull assertion operator(titleの後ろの!)が、値がnullの変数(title)に働いた結果のエラーとしてよく出ます。
なのでNull assertion operatorが使われている箇所を調べていくと早く見つかる可能性が上がると思います。
Null assertion operatorについては
https://dart.dev/guides/language/language-tour#other-operators
こちらで確認できます。

投稿2022/08/12 16:45

moriman

総合スコア615

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問