LateError (LateInitializationError: Field '_state#590051772' has not been initialized.) - flutter

I don't understand why I'm catching an error in _mapController:
final MapController _mapController = MapController();
#override
void initState() {
super.initState();
_mapController.move(widget.latLng, _mapController.zoom);
}

I don't know but try like this ? :
late MapController _mapController;
#override
void initState() {
_mapController = MapController();
_mapController.move(widget.latLng, _mapController.zoom);
super.initState();
}

Try below code
MapController? _mapController;
//or try this-> MapController _mapController;
#override
void initState() {
super.initState();
_mapController.move(widget.latLng, _mapController.zoom);
}

I think the problem arises because you start using the mapcontroller before the build function starts which has the necessary configuration for the map. Try to put this line
_mapController.move(widget.latLng, _mapController.zoom);
inside a method for example (_moveMap) and call it inside the build function when pressing a button.
like this:
child: ElevatedButton(
child:Icon(Icons.location_on_outlined,
color: Colors.blue,) ,
onPressed:_moveMap ,
)),

Related

Non-nullable instance field '_tabController' must be initialized

I frustated over this code
_GenresListState(this.genres);
why it keeps showing the alert of _tabController must be initialized, im already put late constructor but the error is still going, this is the code where i put it:
class _GenresListState extends State<GenresList> with SingleTickerProviderStateMixin {
final List<Genre> genres;
_GenresListState(this.genres);
TabController _tabController;
#override
void initState() {
super.initState();
If you provide to initialize before reading _tabController, you can include late
late TabController _tabController;
#override
void initState() {
super.initState();
_tabController = TabController(length: length, vsync: this)
More about working with tabs
You have to initialize the tab controller, because it needs to populate you current layout with the accurate data needed for the screen to be built.
You can't give it values after the screen has been built, and flutter won't let you do it either because it needs those values.
late TabController tabController;
#override
void initState() {
tabController = TabController(length: 2, vsync: this);
super.initState();
}
#override
void dispose() {
tabController.dispose();
super.dispose();
}
Notice that the value had to be stated in the initState so when the layout is being built, it builds correctly.
For you use case if you want to build with the list of genres, you do something like this
late TabController tabController;
#override
void initState() {
tabController = TabController(length: genres.length, vsync: this);
super.initState();
}
#override
void dispose() {
tabController.dispose();
super.dispose();
}

Initialized in initState but late error in didChangeDependencies

late bool onoff;
#override
void initState() {
super.initState();
_initUser();
}
#override
void didChangeDependencies() {
super.didChangeDependencies();
final pro = Provider.of<Pro>(context);
pro.firstsulonoff = onoff;
}
_initUser() async{
var test = await fireStore.collection('firstsul').doc('firstsul').get();
onoff = test["onoff"];
This is the code that receives Firebase's data from initstate before build is executed and puts it into the firstsulonoff variable of Provider from didChangeDependencies. Late error pops up.
LateInitializationError: Field 'onoff' has not been initialized.
Looks to me that _initUser should be in Pro and the widget should be stateless.

Flutter LateError on controller has not been initialized

When I try to call my widget it's showing an error on the controller that _controller is not initialized I try to set it in initstate.
class CameraApp extends StatefulWidget {
final dynamic loadingWidget;
CameraApp(this.loadingWidget);
_CameraAppState createState() => _CameraAppState();
}
class _CameraAppState extends State<CameraApp> with WidgetsBindingObserver {
late List<CameraDescription> _cameras;
late CameraController _controller;
int _selected = 0;
#override
void initState() {
CameraController _controller;
super.initState();
setupCamera();
WidgetsBinding.instance!.addObserver(this);
}
#override
void dispose() {
WidgetsBinding.instance!.addObserver(this);
_controller.dispose();
super.dispose();
}
#override
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (_controller == null || !_controller.value.isInitialized) {
return;
}
if (state == AppLifecycleState.inactive) {
_controller.dispose();
} else if (state == AppLifecycleState.resumed) {
setupCamera();
}
}
#override
Widget build(BuildContext context) {
if (_controller == null) {
if (widget.loadingWidget != null) {
return widget.loadingWidget;
} else {
return Container(
color: Colors.black,
);
}
} else {
return CameraPreview(_controller);
}
}
Future<void> setupCamera() async {
await [
Permission.camera,
].request();
_cameras = await availableCameras();
var controller = await selectCamera();
setState(() => _controller = controller);
}
selectCamera() async {
var controller =
CameraController(_cameras[_selected], ResolutionPreset.max);
await controller.initialize();
return controller;
}
toggleCamera() async {
int newSelected = (_selected + 1) % _cameras.length;
_selected = newSelected;
var controller = await selectCamera();
setState(() => _controller = controller);
}
}
I am showing this camera on some widgets but don't figure out how to solve this issue. Maybe because of late it's causing an issue. Showing every time when its load i also try to add contoller.initialize(); in initstate but not working
LateError means a variable declared using the late keyword has not been initialized by the time you try to use it, as a general rule, I try to never use the late keyword unless there is no better way to achieve what I want because it tends to cause hard to find errors.
So you have two late variables, _controller and _cameras.
both initialize on the setupCamera method, which is asynchronous and gets called on initState, but the problem I believe is that initState does not wait for them to finish initializing before running build, where you try to read _controller and, because you have yet to assign it, you get a LateError.
If my assertion is correct, it should be a relatively simple fix:
from:
late List<CameraDescription> _cameras;
late CameraController _controller;
to:
List<CameraDescription> _cameras = []; // could also be null I guess.
CameraController _controller = null;
You already have null checks everywhere in case _controller is null, I believe you should take advantage of that so that if build runs before _controller has a value assigned, you get the loading widget.
CameraController _controller = null;
It can't take null value.

Show Alert dialog from state

void initState() {
super.initState();
showAlertDialog(context);
i want to show alertdialog in state
I've tried the following methods but it doesn't work
can you tell me what to do!
You can do by using 'addPostFrameCallback' method.
https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html
void initState() {
super.initState();
WidgetsBinding.instance
.addPostFrameCallback((_) => showAlertDialog(context));
}

How to make a variable final and assign it a value inside initState()

What's the best way to make the AnimationController final in a StatefulWidget, the below codes give error.
final AnimationController _controller; // I want to keep it final
#override
void initState() {
super.initState();
_controller = AnimationController(vsync: this); // error
}
That is not possible. initState cannot be used to initialize final variables.
Even then, there is no way to assign an AnimationController to a final property as it depends on this.
As explained by RĂ©mi Rousselet's answer, you can't.
If you absolutely need _controller to be final (why?), then you could work around it by wrapping it in another object (e.g. a custom class, a List, ...) and mutating that. For example:
class Boxed<T> {
T value;
}
final _controller = Boxed<AnimationController>();
#override
void initState() {
super.initState();
_controller.value = AnimationController(vsync: this);
}
You can now do that with late keyword.
late final AnimationController _controller;
#override
void initState() {
super.initState();
_controller = AnimationController(vsync: this);
}