Uri.parse('https://www.a2rstore.in/api/school/v1/noticeApi.php?id=${widget.s_id}'); got error on widget.s_id - flutter

class Notice extends StatefulWidget {
final String s_id;
const Notice({Key key, this.s_id}) : super(key: key);
#override
_NoticeState createState() => _NoticeState();
}
class _NoticeState extends State<Notice> {
TextEditingController _titleController = new TextEditingController();
var api =
Uri.parse('https://www.a2rstore.in/api/school/v1/noticeApi.php?id=${widget.s_id}');

You can't call the "widget" without the context.
The proper way to do it is by first defining your variable:
class _NoticeState extends State<Notice> {
TextEditingController _titleController = new TextEditingController();
var api;
...
}
And then assigning to it the value either in the build or initState method:
#override
initState(){
api = Uri.parse('https://www.a2rstore.in/api/school/v1/noticeApi.php?id=${widget.s_id}');
}

Related

How to get a value from class in StatefulWidget in flutter?

I am new in flutter and i have some codes to build textfield. i want to make an initial value in textfield but this is from input in another class.
class TextFieldEdit extends StatefulWidget {
TextFieldEdit({
Key? key,
required this.title,
required this.hintTxt,
required this.controller,
required this.defaultTxt,
}) : super(key: key);
final String title, hintTxt;
final controller;
final defaultTxt;
#override
State<TextFieldEdit> createState() => _TextFieldEditState();
}
class _TextFieldEditState extends State<TextFieldEdit> {
TextEditingController _controller = TextEditingController();
#override
void initState() {
super.initState();
_controller.text = defaultTxt;
}
#override
Widget build(BuildContext context) {
return ...
}
in _TextFieldEditState class at the _controller.text i want to get value from defaultTxt in TextFieldEdit class. But how can i send it to _TextFieldEditState class?
the error message is : Undefined name 'defaultTxt'.
Try correcting the name to one that is defined, or defining the name.
Use widget. to access to the variable in constructor:
#override
void initState() {
super.initState();
_controller.text = widget.defaultTxt;
}
To access the widget variable follow widget.variableName
You can do
late final TextEditingController _controller =
TextEditingController.fromValue(
TextEditingValue(text: widget.defaultTxt));
Or use initState to assign the _controller.

How can i access a textEditingController created in another file in flutter

I have two files. File 1 has a nameController and file2 has emailController and passwordController.
I want to access the nameController from file1 in file2 which has the .registerUsingEmailPassword() method .
file1
class UserName extends StatefulWidget {
const UserName({Key? key}) : super(key: key);
#override
State<UserName> createState() => _UserNameState();
}
final _registerFormKey = GlobalKey<FormState>();
final _nameTextController = TextEditingController();
...
Form(
key: _registerFormKey,
child: TextFormField(
controller: _nameTextController,
..
));
}
file2
class SignUp extends StatefulWidget {
const SignUp({Key? key}) : super(key: key);
#override
State<SignUp> createState() => _SignUpState();
}
class _MyEmailState extends State<SignUp> {
final _regFormKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
...
if (_regFormKey.currentState!.validate()) {
User? user = await FireAuth.registerUsingEmailPassword(
name: _nameTextController.text, // I want to access this controller from
file1
email: _emailTextController.text,
password:_passwordTextController.text,
);
}
}
Can somebody help please.
You must be going from file 1 to file 2 through navigator, pass the data from nameController as an argument to the Navigator like,
Navigator.of(context).pushNamed('file2Route', arguments:nameController.text);
Get the text in file2 as
var nameText = ModalRoute.of(context)!.settings.arguments as String;
Then simply use the nameText in registerUsingEmailPassword() method.
If this is not the case, please do specify.
Cheers!!
class SignUp extends StatefulWidget {
final TextEditingController nameController; // pass via constructor
const SignUp({Key? key,required TextEditingController nameController}) : super(key: key);
#override
State<SignUp> createState() => _SignUpState();
}
then use in build like this
if (_regFormKey.currentState!.validate()) {
User? user = await FireAuth.registerUsingEmailPassword(
name: widget.nameController.text, // use like this
email: _emailTextController.text,
password:_passwordTextController.text,
);

Accessing route parameters in Dart and flutter

I am sending 2 parameters restaurantDetails.rest_latitude and restaurantDetails.rest_longitude to a class to get the map details
BarMapWidget(restaurantDetails.rest_latitude, restaurantDetails.rest_longitude),
In the class I have a stateful widget
class BarMapWidget extends StatefulWidget {
String restLatitude;
String restLongitude;
BarMapWidget(this.restLatitude, this.restLongitude, {Key? key}) : super(key: key);
#override
_BarMapWidgetState createState() => _BarMapWidgetState();
}
I am passing a string so I know I have to make it a double
Here is the extended class,
class _BarMapWidgetState extends State<BarMapWidget> {
// Destination Longitude
final double _destLatitude = restLatitude;
final double _destLongitude = restLongitude;
void initState() {
// Add destination marker
_addMarker(
LatLng(_destLatitude, _destLongitude),
"destination",
// BitmapDescriptor.defaultMarkerWithHue(90),
BitmapDescriptor.defaultMarker
);
super.initState();
}
Which give me a Undefined name 'restLatitude'. and Undefined name 'restLongitude'.
How can I access the variable in BarMapWidget?
Your restLatitude and restLongitude on widget level, try using widget.variableName. Also, those aren't matching datatype.
If you need double convert BarMapWidget variable to double.
To initialize on state level you can trick with using late or use initState;
class BarMapWidget extends StatefulWidget {
final double restLatitude;
final double restLongitude;
const BarMapWidget(this.restLatitude, this.restLongitude, {Key? key})
: super(key: key);
#override
_BarMapWidgetState createState() => _BarMapWidgetState();
}
class _BarMapWidgetState extends State<BarMapWidget> {
// Destination Longitude
late final double _destLatitude = widget.restLatitude;
late final double _destLongitude = widget.restLongitude;
Just add in
widget.restLatitude

LateInitializationError: Field 'authProvider' has not been initialized

I got the error LateInitializationError: Field 'authProvider' has not been initialized. for the following:
class HomePage extends StatefulWidget {
HomePage({Key? key}) : super(key: key);
#override
State createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
final FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
final ScrollController listScrollController = ScrollController();
late AuthProvider authProvider;
String? currentUserId;
late MainProvider mainProvider;
Debouncer searchDebouncer = Debouncer();
StreamController<bool> btnClearController = StreamController<bool>();
TextEditingController searchBarTec = TextEditingController();
#override
void initState() {
super.initState();
mainProvider = context.read<MainProvider>();
if (authProvider.getUserFirebaseId()!.isNotEmpty == true) {
currentUserId = authProvider.getUserFirebaseId()!;
} else {
return null;
}
registerNotification();
listScrollController.addListener(scrollListener);
}
//more code below
This code is from a Demo: Chat App with Flutter
How do I initialize the fields for authProvider, mainProvider etc?
Late initialization error means that a variable marked as late (in your case authProvider) was not initialized before it was accessed.
on a widget the first thing you execute is the constructor and then you execute the initstate. your constructor has nothing and initstate reads authProvider.getUserFirebaseId().
If you take a look at the video's github page, you will see that before calling authProvider, they initialize it by running the following line:
authProvider = context.read<AuthProvider>();
homeProvider = context.read<HomeProvider>();
If you are following a tutorial, the tutorial is either outdated or not complete if it has this sort of error.

Stateful widget, class marked #immutable

I got a Visual Studio warning on my class (below) saying "This class (or a class which this class inherits from) is marked as '#immutable', but one or more of its instance fields are not final: UserSignIn._email", but I cannot mark this argument as final because I initialise it in the constructor
Without final :
class UserSignIn extends StatefulWidget {
TextEditingController _email;
UserSignIn({String emailInput}) {
this._email = TextEditingController(text: (emailInput ?? ""));
}
#override
_UserSignInState createState() => _UserSignInState();
}
class _UserSignInState extends State<UserSignIn> {
#override
Widget build(BuildContext context) {
...
}
}
How to do this ?
Thank you
You should put the TextEditingController in the state class and initialise it in the initState method, like this.
And keep in mind that the StatefulWidget can be different every time when the widget tree is changed, so don't put anything in there that is not immutable.
Keep everything dynamic in the State class
class UserSignIn extends StatefulWidget {
final String emailInput;
const UserSignIn({Key key, this.emailInput}) : super(key: key);
#override
_UserSignInState createState() => _UserSignInState();
}
class _UserSignInState extends State<UserSignIn> {
TextEditingController _controller;
#override
void initState() {
_controller = TextEditingController(text: widget.emailInput);
super.initState();
}
...
}