前提・実現したいこと
APIを受け取ったデータを画面上に表示させる処理を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
The following _CastError was thrown building Consumer<Status>(dirty, dependencies: [_InheritedProviderScope<Status?>]): Null check operator used on a null value The relevant error-causing widget was: Consumer<Status> Consumer:file:///Users/username/StudioProjects/app_name/lib/screens/user_status.dart:55:14
該当のソースコード
APIを叩く処理(services/user_status.dart)
dart
1import 'package:flutter/material.dart'; 2import 'package:delivery_kun/models/user_status.dart'; 3import 'package:dio/dio.dart' as Dio; 4import 'package:intl/intl.dart'; 5import 'dio.dart'; 6 7class Status extends ChangeNotifier { 8 UserStatus? _userStatus; 9 10 UserStatus get status => _userStatus!; 11 12 String getDate() { 13 return DateFormat('yyyyMMdd').format(DateTime.now()).toString(); 14 } 15 16 17 void getStatus(int user_id) async { 18 String date = getDate(); 19 20 Dio.Response response = await dio() 21 .get('/status', queryParameters: {'date': date, 'user_id': user_id}); 22 _userStatus = UserStatus.fromJson(response.data); 23 24 notifyListeners(); 25 } 26}
モデル(models/user.dart)
dart
1class UserStatus { 2 final String onlineTime; 3 final int daysEarningsTotal; 4 final int daysEarningsQty; 5 final String vehicleModel; 6 final int actualCost; 7 8 UserStatus( 9 {required this.onlineTime, 10 required this.daysEarningsTotal, 11 required this.daysEarningsQty, 12 required this.actualCost, 13 required this.vehicleModel}); 14 15 UserStatus.fromJson(Map<String, dynamic> json) 16 : onlineTime = json['data']['summary']['onlineTime'], 17 daysEarningsTotal = json['data']['summary']['daysEarningsTotal'], 18 actualCost = json['data']['summary']['actualCost'], 19 daysEarningsQty = json['data']['summary']['daysEarningsQty'], 20 vehicleModel = json['data']['user']['vehicleModel']; 21} 22
screenは長いため該当箇所のみです
getterの得たstatus.statusにアクセスするとエラーが起きるようです。
dart
1class LoggedInUserStatus extends StatelessWidget { 2 LoggedInUserStatus({required this.user_id}); 3 4 int user_id; 5 6 7 Widget build(BuildContext context) { 8 context.read<Status>().getStatus(user_id); 9 return Container( 10 child: Consumer<Status>( 11 builder: (context, status, child) => Column( 12 children: <Widget>[ 13 Container( 14 alignment: Alignment.center, 15 padding: EdgeInsets.only(top: 20), 16 child: Text( 17 DateFormat('yyyy年M月d日').format(DateTime.now()), 18 textAlign: TextAlign.center, 19 style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold), 20 )), 21 Container( 22 height: 250, 23 color: Colors.grey, 24 ), 25 Container( 26 margin: EdgeInsets.only(right: 15, left: 15), 27 child: Column( 28 children: [ 29 Container( 30 alignment: Alignment.topLeft, 31 margin: EdgeInsets.only(top: 10), 32 child: Text( 33 'ステータス', 34 style: TextStyle( 35 fontSize: 25, 36 ), 37 )), 38 Table( 39 border: TableBorder( 40 bottom: 41 BorderSide(width: 0.5, color: Color(0xFF000000)), 42 ), 43 children: [ 44 TableRow(children: [ 45 Text( 46 '時間', 47 style: TextStyle(color: Colors.grey), 48 ), 49 Text('数', style: TextStyle(color: Colors.grey)) 50 ]), 51 TableRow(children: [ 52 TableCell( 53 child: SizedBox( 54 height: 50, 55 child: Text( 56 status.status.onlineTime, 57 style: TextStyle( 58 fontSize: 20, 59 ), 60 ), 61 ), 62 ), 63 TableCell( 64 child: SizedBox( 65 height: 50, 66 child: Text( 67 status.status.daysEarningsQty.toString(), 68 style: TextStyle( 69 fontSize: 20, 70 ), 71 ), 72 ), 73 ), 74 ]), 75 ]), 76 Container( 77 alignment: Alignment.topLeft, 78 margin: EdgeInsets.only(top: 10), 79 child: Text( 80 '明細', 81 style: TextStyle( 82 fontSize: 30, 83 ), 84 )), 85 Table(children: [ 86 TableRow(children: [ 87 Text( 88 '売上', 89 style: TextStyle( 90 color: Colors.black, 91 fontSize: 20, 92 ), 93 ), 94 Text(status.status.daysEarningsTotal.toString(), 95 textAlign: TextAlign.right, 96 style: TextStyle( 97 color: Colors.black, 98 fontSize: 20, 99 )) 100 ]), 101 TableRow(children: [ 102 TableCell( 103 child: SizedBox( 104 height: 50, 105 child: Text( 106 '支出', 107 style: TextStyle( 108 fontSize: 20, 109 ), 110 ), 111 ), 112 ), 113 TableCell( 114 child: SizedBox( 115 height: 50, 116 child: Text( 117 status.status.actualCost.toString(), 118 textAlign: TextAlign.right, 119 style: TextStyle( 120 fontSize: 20, 121 ), 122 ), 123 ), 124 ), 125 ]), 126 TableRow(children: [ 127 TableCell( 128 child: SizedBox( 129 height: 50, 130 child: Text( 131 '利益', 132 style: TextStyle( 133 fontSize: 20, 134 ), 135 ), 136 ), 137 ), 138 TableCell( 139 child: SizedBox( 140 height: 50, 141 child: Text( 142 (status.status.daysEarningsTotal - 143 status.status.actualCost) 144 .toString(), 145 textAlign: TextAlign.right, 146 style: TextStyle( 147 fontSize: 20, 148 ), 149 ), 150 ), 151 ), 152 ]), 153 ]), 154 ElevatedButton( 155 onPressed: () {}, 156 child: Text( 'ボタン'), 157 style: ElevatedButton.styleFrom( 158 primary: Colors.grey, 159 minimumSize: Size(double.infinity, 55), 160 ), 161 ), 162 ], 163 )) 164 ], 165 ), 166 ), 167 ); 168 } 169}
試したこと
nullsafety(?)など試しましたがエラーは変わりませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/17 03:27