Reputation: 868
In my mobile application, I am initializing a Stateful widget from another widget but I always get an exception
[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: LateInitializationError: Field '_customAppLoaderState@64195267' has not been initialized
Below is the code for custom_loader.dart
import 'package:SMedoApp/util/app_textstyles.dart';
import 'package:SMedoApp/util/color_constants.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class CustomAppLoader extends StatefulWidget {
// const CustomAppLoader({Key? key}) : super(key: key);
late final _CustomAppLoaderState _customAppLoaderState;
@override
State<CustomAppLoader> createState() {
_customAppLoaderState=_CustomAppLoaderState();
return _customAppLoaderState;
}
void setLoaderVisible(bool _visibility){
_customAppLoaderState.setVisibility(_visibility);
}
void setProgressPerc(double progress){
_customAppLoaderState.setProgressValue(progress: progress);
}
void setCancelToken(CancelToken cancelToken) {
_customAppLoaderState.setCancelToken(cancelToken: cancelToken);
}
}
class _CustomAppLoaderState extends State<CustomAppLoader> {
bool isLoaderVisible=false;
double _progress=0.0;
CancelToken? _cancelToken;
bool isCancelButtonVisible=false;
@override
Widget build(BuildContext context) {
return Visibility(
visible: isLoaderVisible,
child: Center(
child: Container(
color: ColorConstants.black.withOpacity(0.8),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SpinKitWave(
size: 50,
color: ColorConstants.white,
type: SpinKitWaveType.start
),
SizedBox(
height: 50,
),
Container(
width: 200,
child: LinearProgressIndicator(
backgroundColor: ColorConstants.white,
valueColor: new AlwaysStoppedAnimation<Color>(ColorConstants.facebook_blue),
value: _progress,
minHeight: 2,
),
),
SizedBox(
height: 10,
),
Visibility(
visible: isCancelButtonVisible,
child: TextButton(onPressed: (){
_cancelToken?.cancel();
if(_cancelToken!.isCancelled)
Navigator.pop(context);
}, child: Text(AppLocalizations.of(context)!.cancel, style: AppTextStyle.whiteOnBlackSmallWhite(context),), ),
)
],
)),
),
);
}
void setVisibility(bool _visibility){
setState(() {
isLoaderVisible=_visibility;
});
}
void setProgressValue({required double progress}) {
setState(() {
_progress=progress;
}
);
}
void setCancelToken({required CancelToken cancelToken}) {
setState(() {
_cancelToken=cancelToken;
isCancelButtonVisible=true;
});
}
}
And this is how I invoke custom_loader from another widget
CustomAppLoader loader=CustomAppLoader();
loader.setProgressPerc(0.25);
Where am I going wrong? (I am new to flutter/ dart).
Upvotes: 0
Views: 135
Reputation: 23312
createState()
is not called yet on initialization of the CustomAppLoader
, so when you call setProgressPerc
the state doesn't exist yet. It's also not really common to save the state in a variable and using it like that.
My IDE also actually suggest that you shouldn't do any logic in the createState():
Upvotes: 1