为什么该值在第一秒显示为空,而在第一秒后显示正确值...
我在我的集团 Cubit
类中有以下方法:
CellsResponse cellResponse;
String mcc = "";
String mnc = "";
StatefulElement _element;
bool get mounted => _element != null;
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
CellsResponse cellsResponse;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
String platformVersion = await CellInfo.getCellInfo;
final body = json.decode(platformVersion);
cellsResponse = CellsResponse.fromJson(body);
CellType currentCellInFirstChip = cellsResponse.primaryCellList[0];
if (currentCellInFirstChip.type == "LTE") {
mcc =
"LTE mcc = " + currentCellInFirstChip.lte.network.mcc.toString();
mnc =
"LTE mnc = " + currentCellInFirstChip.lte.network.mnc.toString();
} else if (currentCellInFirstChip.type == "NR") {
mcc =
"NR mcc = " + currentCellInFirstChip.nr.network.mcc.toString();
mnc =
"LTE mnc = " + currentCellInFirstChip.nr.network.mnc.toString();
} else if (currentCellInFirstChip.type == "WCDMA") {
mcc = "WCDMA mcc = " +
currentCellInFirstChip.wcdma.network.mcc.toString();
mnc =
"LTE mnc = " + currentCellInFirstChip.wcdma.network.mnc.toString();
} else if (currentCellInFirstChip.type == "GSM") {
mcc = currentCellInFirstChip.gsm.network.mcc.toString();
mnc =
"LTE mnc = " + currentCellInFirstChip.gsm.network.mnc.toString();
// print('currentMCC = ' + mcc);
// print('currentCqi = ' + mnc);
}
} on PlatformException {
cellResponse = null;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
cellResponse = cellsResponse;
emit(AppSetCellResponseState());
}
当我在下面的类无状态小部件中调用该值时:
DataCell(
CustomText(
padding: EdgeInsets.only(top: 6),
text: cubit.mcc,
color: goldDefaultColor,
),
),
因此,当我尝试获取构建小部件中的值时:
print('currentMCC is = ' + AppCubit.get(context).mcc);
它显示值为空,一秒钟后在日志中显示正确的值,如下所示:
I/flutter (23753): currentMCC is =
I/flutter (23753): currentMCC is = 602
这是下面的全屏类:
import 'package:airforce/shared/cubit/app_cubit.dart';
import 'package:airforce/shared/cubit/app_states.dart';
import 'package:airforce/shared/widgets/serving_cell_widget/gsm/servingCellTable.dart';
import 'package:flutter/material.dart';
import 'package:airforce/shared/widgets/custom_text.dart';
import 'package:airforce/shared/styles/colors.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class SlotScreen extends StatelessWidget{
@override
Widget build(BuildContext context) {
print('currentMCC is = ' + AppCubit.get(context).mcc);
return BlocConsumer<AppCubit, AppStates>(
listener: (context, state) {},
builder: (context, state) {
var cubit = AppCubit.get(context);
return Container(
child: Container(
color: Colors.black.withOpacity(.4),
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
controller: cubit.scrollController,
child: Column(
children: [
SizedBox(
height: 100,
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
padding: EdgeInsets.all(15),
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Color(0xFFA2A2A2)),
borderRadius: BorderRadius.vertical(
top: Radius.circular(20.0),
bottom: Radius.circular(5.0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
CustomText(
text: 'Sim information',
fontWeight: FontWeight.bold,
),
LayoutBuilder(
builder: (context, constraints) => Row(
children: [
Container(
child: Image.asset(
'assets/images/vodafone-logo.png',
),
width: constraints.maxWidth * 0.25,
),
Expanded(
child: DataTable(
headingRowHeight: 20,
columnSpacing: 40,
dataRowHeight: 20,
columns: [
DataColumn(
label: CustomText(
padding: EdgeInsets.only(top: 3),
text: 'OPERATOR',
)),
DataColumn(
label: Row(
children: <Widget>[
CustomText(
padding:
EdgeInsets.only(top: 3),
text: 'vodafone',
color: goldDefaultColor,
),
],
),
),
],
rows: [
DataRow(cells: [
DataCell(Row(
children: <Widget>[
CustomText(
text: 'MCC',
padding:
EdgeInsets.only(top: 6),
),
],
)),
DataCell(
CustomText(
padding: EdgeInsets.only(top: 6),
text: cubit.mcc,
color: goldDefaultColor,
),
),
]),
DataRow(cells: [
DataCell(CustomText(
padding: EdgeInsets.only(top: 6),
text: 'MNC',
)),
DataCell(CustomText(
padding: EdgeInsets.only(top: 6),
text: '02',
color: goldDefaultColor,
)),
]),
],
),
),
],
),
),
],
),
),
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
padding: EdgeInsets.all(15),
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Color(0xFFA2A2A2)),
borderRadius: BorderRadius.vertical(
top: Radius.circular(20.0),
bottom: Radius.circular(5.0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
CustomText(
text: 'Sim information',
fontWeight: FontWeight.bold,
),
DataTable(
headingRowHeight: 20,
columnSpacing: 40,
dataRowHeight: 20,
columns: [
DataColumn(
label: CustomText(
padding: EdgeInsets.only(top: 6),
text: 'Sim operator',
)),
DataColumn(
label: Row(
children: <Widget>[
CustomText(
padding: EdgeInsets.only(top: 6),
text: 'sim info',
color: goldDefaultColor,
),
],
),
),
],
rows: [
DataRow(cells: [
DataCell(Row(
children: <Widget>[
CustomText(
text: 'ICCID',
padding: EdgeInsets.only(top: 6),
),
],
)),
DataCell(
CustomText(
text: 'iccid',
color: goldDefaultColor,
padding: EdgeInsets.only(top: 6),
)),
]),
DataRow(cells: [
DataCell(
CustomText(
text: 'IMEI',
padding: EdgeInsets.only(top: 6),
),
),
DataCell(CustomText(
text: '123456789',
color: goldDefaultColor,
padding: EdgeInsets.only(top: 6),
)),
]),
DataRow(cells: [
DataCell(CustomText(
text: 'SIM IMSI',
padding: EdgeInsets.only(top: 6),
)),
DataCell(CustomText(
padding: EdgeInsets.only(top: 6),
text: '123456789',
color: goldDefaultColor,
)),
]),
],
),
],
),
),
),
Padding(
padding: const EdgeInsets.all(10),
child: Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: Color(0xFFA2A2A2)),
borderRadius: BorderRadius.vertical(
top: Radius.circular(15.0),
bottom: Radius.circular(5.0)),
),
child: GSMServingCellTable(),
),
),
Padding(
padding: const EdgeInsets.all(10),
child: Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: Color(0xFFA2A2A2)),
borderRadius: BorderRadius.vertical(
top: Radius.circular(15.0),
bottom: Radius.circular(5.0)),
),
child: GSMServingCellTable(),
),
),
],
),
),
),
);
});
}
}
它在 bloc cubit 类中读取得很好,但是当我尝试在屏幕中调用它时,我发现了这个问题....