Flutter: I am missing the image parameter - flutter

I have a class where my object are, I tried calling it out in another file where I want to make use of the properties, It is telling me required named parameter 'image' must be provided.
class _homeState extends State<home> {
var _currentIndex = 0;
final screens = [
HomePage(),
Categories(),
const NearYou(image: '', title: '', subtitle: '',),
Cart(),
Profile()
];
I really don't know what to put in those parameters above
Also, It is saying: Found this candidate, but the arguments don't match.
Will I also call my class in that file?
class NearYou extends StatefulWidget {
final String image;
final String title;
final String subtitle;
const NearYou(
{Key? key,
required this.image,
required this.title,
required this.subtitle})
: super(key: key);

Related

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

How to make widget without '?' and 'required'

so im working on company project and realize the widget doesn't need Key? and required costructor, and when i try to make it like that it occur red underline.
my project i want to remove the required and '?'
example of what i want
It should view like that:
class CustomButton extends StatelessWidget {
final double? height;
final double width;
final String title;
final double margin;
final Function() onPressed;
const CustomButton ({
Key? key,
this.height,
required this.width,
required this.title,
required this.margin,
required this.onPressed,
}) : super (key: key);
}
If you want to preserve the non-nullable state of your class, you have to pass an initial value for the CustomButton height property.
class CustomButton extends StatelessWidget {
final double height;
final double width;
final String title;
final double margin;
final Function() onPressed;
const CustomButton ({
Key? key,
this.height = 100,
required this.width,
required this.title,
required this.margin,
required this.onPressed,
}) : super (key: key);
}
you have probably defined height as final and that's why it is asking it.
define height as
double? height

Flutter Stateful Constructor Const Problem

I'm following tutorial on udemy and i found this weird error and i already read some doccumentation and i still didnt find the right answer to have constructor with initail value.
her is my code
class MapScreen extends StatefulWidget {
final PlaceLocation initialLocation;
final bool isSelecting;
const MapScreen({Key? key, this.initialLocation = PlaceLocation( //here there is an error "The default value of an optional parameter must be constant. (Documentation)"
latittude: 37.422,
longitude: -122.084,
address: "Example stree no 1",
), this.isSelecting = false}) : super(key: key);
#override
_MapScreenState createState() => _MapScreenState();
}
however if i delete those const new error come out
class MapScreen extends StatefulWidget {
final PlaceLocation initialLocation;
final bool isSelecting;
MapScreen({Key? key, this.initialLocation = PlaceLocation( //The default value of an optional parameter must be constant. (Documentation)
latittude: 37.422,
longitude: -122.084,
address: "Jl. Imambonjol",
), this.isSelecting = false}) : super(key: key);
#override
_MapScreenState createState() => _MapScreenState();
}
i also tried to follow the instruction and modify my code like this:
class MapScreen extends StatefulWidget {
final PlaceLocation initialLocation;
final bool isSelecting;
MapScreen({
this.initialLocation =
const PlaceLocation(latitude: 37.422, longitude: -122.084),
this.isSelecting = false,
});
#override
_MapScreenState createState() => _MapScreenState();
}
but the error still came out
I was able to fix your mistake, below I will attach the solution code and a screenshot of the console:
// Fake model:
class PlaceLocation {
final double latittude;
final double longitude;
final String address;
// Make this constructor const to solve your problem:
const PlaceLocation(
{required this.latittude,
required this.longitude,
required this.address});
}
class MapScreen extends StatefulWidget {
final PlaceLocation initialLocation;
final bool isSelecting;
// Also don't forget to put const before PlaceLocation() in this constructor:
const MapScreen(
{Key? key,
this.initialLocation = const PlaceLocation(
latittude: 37.422,
longitude: -122.084,
address: "Example stree no 1",
),
this.isSelecting = false})
: super(key: key);
#override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
#override
Widget build(BuildContext context) {
// Console test to check default values:
print("lattitude: " +
MapScreen().initialLocation.latittude.toString() +
"\nlongitude: " +
MapScreen().initialLocation.longitude.toString() +
"\naddress: " +
MapScreen().initialLocation.address.toString());
return Scaffold(body: Container());
}
}
All you have to do is make your model (PlaceLocation) constructor const.

Dart: Error: Setter not found (when defining constructor of a class)

I get the following error with this very simple constructor:
class MyHomePage extends StatefulWidget {
MyHomePage(String title, String lang, {Key key}) {
this.title = title;
this.lang = lang.substring(0,2).toLowerCase();
super(key: key);
}
final String title;
final String lang;
#override
_MyHomePageState createState() => _MyHomePageState();
}
The error message:
Compiler message:
lib/main.dart:34:10: Error: Setter not found: 'title'.
this.title = title;
^^^^^
lib/main.dart:35:10: Error: Setter not found: 'lang'.
this.lang = lang.substring(0,2).toLowerCase();
^^^^
lib/main.dart:36:5: Error: Can't use 'super' as an expression.
To delegate a constructor to a super constructor, put the super call as an initializer.
super(key: key);
^
lib/main.dart:34:10: Error: The setter 'title' isn't defined for the class 'MyHomePage
Why is that? I don't understand what that mean.
Is there a comprehensive explanation about how to define constructors in dart? I couldn't find one on the dart.dev site.
Edit:
After editing the code to the following:
class MyHomePage extends StatefulWidget {
MyHomePage(String title, String lang, {Key key}) {
title = title;
lang = lang.substring(0,2).toLowerCase();
super(key: key);
}
final String title;
final String lang;
#override
_MyHomePageState createState() => _MyHomePageState();
}
I have the following complaints from the compiler:
Compiler message:
lib/main.dart:36:5: Error: Can't use 'super' as an expression.
To delegate a constructor to a super constructor, put the super call as an initializer.
super(key: key);
^
lib/main.dart:39:16: Error: Final field 'title' is not initialized.
Try to initialize the field in the declaration or in every constructor.
final String title;
^^^^^
lib/main.dart:40:16: Error: Final field 'lang' is not initialized.
Try to initialize the field in the declaration or in every constructor.
final String lang;
There is special syntax for initialising final variables in a constructor, answered here:
https://stackoverflow.com/a/42864979/64435
Using this syntax, the working code is:
class MyHomePage extends StatefulWidget {
MyHomePage(String title, String lang, {Key key})
: this.title = title,
this.lang = lang.substring(0, 2).toLowerCase(),
super(key: key);
final String title;
final String lang;
#override
_MyHomePageState createState() => _MyHomePageState();
}

Make Function parameter optional in custom widget flutter

I try to create some custom widgets with some parameters in the constructor. This widget has some optional and required parameters.
how can make Function type parameter optional in my Widget.
class TextInputWithIcon extends StatefulWidget {
final String iconPath;
final String placeHolder;
final Function(bool) onFocusChange;
const TextInputWithIcon(
{Key key,
#required this.iconPath,
this.placeHolder = "",
this.onFocusChange})
: super(key: key);
#override
_TextInputWithIconState createState() => _TextInputWithIconState();
}
class _TextInputWithIconState extends State<TextInputWithIcon> {
#override
Widget build(BuildContext context) {
return MY_WIDGET;
}
}
Optional parameters can be either positional or named, but not both.
Named parameters are optional by default so you don't have to assign the default value.
If a parameter is optional but can’t be null, provide a default value.
With null safety
class TextInputWithIcon extends StatefulWidget {
final String iconPath;
final String placeHolder;
final Function(bool)? onFocusChange; // nullable and optional
const TextInputWithIcon(
{Key? key,
required this.iconPath, // non-nullable and required
this.placeHolder = "", // non-nullable but optional with a default value
this.onFocusChange, // nullable and optional
})
: super(key: key);
#override
_TextInputWithIconState createState() => _TextInputWithIconState();
}
Without null safety
const TextInputWithIcon(
{Key key,
#required this.iconPath,
this.placeHolder = "",
this.onFocusChange
})
: super(key: key);
Usage:
void _focusChanged(bool value) {
// using null-aware operator (for both with and without null safety)
onFocusChange?.call(value);
// or without null-aware operator
// with null safety
if(onFocusChange != null) {
onFocusChange!(value);
}
// without null safety
if(onFocusChange != null) {
onFocusChange(value);
}
}
Dart 2.17 update:
Although it often makes sense to place positional arguments first, named arguments can be placed anywhere in the argument list when it suits your API:
repeat(times: 2, () {
...
});
Have a look at Optional Parameters to understand better.
Edit: Thank you Jonah Williams to clarification.
You can use a default value that does nothing:
class TextInputWithIcon extends StatefulWidget {
final String iconPath;
final String placeHolder;
final Function(bool) onFocusChange;
const TextInputWithIcon(
{Key key,
#required this.iconPath,
this.placeHolder = "",
this.onFocusChange = _dummyOnFocusChange})
: assert(onFocusChange != null), super(key: key);
#override
_TextInputWithIconState createState() => _TextInputWithIconState();
static dynamic _dummyOnFocusChange(bool val) {}
}
I created a static named function instead of just a closure as a default value because closures are not const and currently default values need to be const.
I added the assert(...) to ensure that an error is shown when null is passed explicitly.
Another option if you don't like named parameters (like me :/) is:
function_name (argument1, [argument2]) {
// statements
}
arguments in brackets are optional.
source