Flutter: change name of named constructor argument - flutter

I am creating a widget in flutter like below:
class CatalogWidget extends StatelessWidget {
final String errorMsg;
final Function receiveAgainCallbackyyyyyyyyyy;
const CatalogWidget({this.errorMsg, this.receiveAgainCallbackyyyyyyyyyy});
As you can see in named constructor receiveAgainCallbackyyyyyyyyyy is long name now my question is here?
How can i use long name argument but when i want to initialize this constructor i see short one like this:
CatalogWidget(errorMsg: "test", callback: function)
I know this is mistake:
const CatalogWidget({this.errorMsg, callback})
: this(receiveAgainCallbackyyyyyyyyyy);
But i don't remember what is the correct way

Like this?
class CatalogWidget extends StatelessWidget {
final String errorMsg;
final Function receiveAgainCallbackyyyyyyyyyy;
const CatalogWidget({this.errorMsg, Function callback})
: receiveAgainCallbackyyyyyyyyyy = callback;
}

You can do something like this:
const CatalogWidget({this.errorMsg, Function callback})
: this.receiveAgainCallbackyyyyyyyyyy = callback;

Related

Can't use extension on BuildContext

I wrote this code to get access to my global scope more comfortable.
extension DepContextExtension on BuildContext {
IGlobalDependency get global => read<IGlobalDependency>();
}
I had this code to use my global dependensy
final globalScope = context.read<IGlobalDependency>();
I change it to this
final globalScope = context.global();
but i got error, that method global isn't defined for the type BuildContext. What i did wrong and how i can fix this issue?
I get globalSopce inside WidgetModel Factory from library Elementary
MainAppScreenWidgetModel mainAppScreenWidgetModelFactory(BuildContext context) {
final globalScope = context.global();
return MainAppScreenWidgetModel(
MainAppScreenModel(
globalScope.baseBloc,
),
);
}
this factory i put inside my Widget like this
class MainAppScreenWidget extends ElementaryWidget<MainAppScreenWidgetModel> {
const MainAppScreenWidget({
Key? key,
WidgetModelFactory wmFactory = mainAppScreenWidgetModelFactory,
}) : super(key: key, wmFactory);
You've declared a property so to access it just use its name:
final globalScope = context.global;

Modifying Lists inside of classes - Flutter, Riverpod, Hooks

I would like to have a class that contains a list of another class and be able to update it using Riverpod/Hooks. I'm trying to figure out the best syntax or even way to approach the issue.
I'm starting with a StateProvider to make it simpler and thought to use the List method .add() to add to the list but I'm getting the error: "The method 'add' can't be unconditionally invoked because the receiver can be 'null'." or null check operator used on null value (when I add the !)- so I think I'm not writing it correctly and I'm sure it has to do with the Riverpod or even Dart syntax.
Any suggestions or resources would be appreciated. Thanks in advance.
So for example:
Session({this.title, this.sessionThings})
String? title;
List<SessionThings> sessionThings;
}
class SessionThings{
SessionThings({this.title, this.time})
String? title;
double? time;
}
ref.read(buildExSessionProvider.notifier).state = Session()..sessionThings.add(SessionThings(title: 'something', time: 20);
using:
hooks_riverpod: ^1.0.3
flutter_hooks: ^0.18.3
Do not access state outside a notifier. Something like this could work:
class SessionStateNotifier extends StateNotifier<Session?> {
SessionStateNotifier() : super(null);
void create(String title) => state = Session(title: title);
void add(SessionThings things) =>
state = Session(title: state?.title, sessionThings: [...state?.sessionThings ?? [], things]);
}
Use with hooks
class MyApp extends HookConsumerWidget {
#override
Widget build(BuildContext context, WidgetRef ref) {
final state = useState(0);
final Session? session = ref.watch(sessionProvider);
final SessionStateNotifier sessionStateNotifier = ref.watch(sessionProvider.notifier);
return <widget>
}
}
``

How to get Variable Value from Variable inside Void Function and Use Those Variable to Widget

As the title says. so I have a void function like the code below
Class Routing{
// another code
void _showRouteDetails(here.Route route) {
int estimatedTravelTimeInSeconds = route.durationInSeconds;
int lengthInMeters = route.lengthInMeters;
String minuteDetails = _formatTime(estimatedTravelTimeInSeconds);
String lengthDetails = _formatLength(lengthInMeters);
//
// The Problem is right here
// I need passing this both variable
// minutesDetails and lengthDetails to MapScreen class
// I've trying with code below
//
MapScreen(minuteRoutes: minuteDetails, lengthRoutes: lengthDetails);
_stringReturn("$minuteDetails", '$lengthDetails');
}
// another code
}
minuteDetails and lengthDetails value is String.
I have tried passing both variables into my MapScreen class (StatelessWidget) like the code below
MapScreen(minuteRoutes: minuteDetails, lengthRoutes: lengthDetails);
but isn't working. and I tried with
_stringReturn("$minuteDetails", '$lengthDetails');
The code above is works but I don't know how to use those strings to apply to widgets
My MapScreen class example
class MapScreen extends StatelessWidget{
const MapScreen({
Key key,
this.minuteRoutes,
this.lengthRoutes,
}) : super(key: key);
final minuteRoutes, lengthRoutes;
Future<void> _stringReturn(String min, String length) async {
print("Test: $min $length");
var minRoutes = min;
var lenRoutes = length;
}
// the future void work fine for only print the variable
// but can't call the lenRoutes and minRoutes variable
#override
Widget build(BuildContext context) {
return Container(
child: Text(minuteRoutes??"Empty"),
);
};
}
Any suggestions for this? Thanks before
I think you are trying to navigate to another screen with some data from previous screen. If this is the case, refer this docs :
https://flutter.dev/docs/cookbook/navigation/passing-data

how to pass a function with callback type in Dart?

say for example I have a custom text field class like this
class CustomTextField extends StatelessWidget {
final Function onSubmit;
CustomTextField({
required this.onSubmit,
});
#override
Widget build(BuildContext context) {
return TextField(
onSubmitted: (value) => onSubmit(value),
);
}
}
as you can see, I need to pass a function to get the submitted text string value from text field. and I can use it like this
CustomTextField (onSubmit: () {
print("something");
})
but I don't have any error when I write my code like that. what I want is like this
CustomTextField (onSubmit: (myValue) { // <--- if I don't write 'myValue' in here it will have error
print(myValue);
})
I want my code to give me error if I don't write a variable name (string) inside the parentheses
how to do that?
You need to change the type of onSubmit from a general function to a function that takes in a String parameter like so.
final Function(String value) onSubmit;

Flutter: Undefined name Try correcting the name to one that is defined, or defining the name

I am facing a few strange issues with Flutter. I do have very little knowledge about Flutter. I am learning it.
class ViewOtherProfile extends StatefulWidget {
final String userName;
final int points;
const ViewOtherProfile({
#required this.userName,
#required this.points,
});
You can see i am getting userName and Points data as argument.
I want to print this argument in the page. Like this
class _ViewOtherProfileState extends State<ViewOtherProfile> {
..........
void initState(){
print(points);
deviceInfo();
super.initState();
print(userName);
]);
}
............
Now problem is i am getting error.
Undefined name 'userName'.
Try correcting the name to one that is defined, or defining the name.
Any reason why i am getting this error and how i can resolve it.
Thanks to #jamesdlin
I tried to put it like this
print(ViewOtherProfile.userName);
but now i am getting another error.
Instance member 'userName' can't be accessed using static access.
There are two main types of widgets in Flutter. StatelessWidget and StatefullWidget. A StatelessWidget is built only one when the UI builds and is never rebuilt. Meanwhile, a StatefulWidget can be rebuilt every time if a call for setState is made.
Hence, for a StatefulWiget, there is a need to track the state of the class by extending the main class with a State class.
You have to note that the scope of variables in those two types of widgets can vary. For example...
class ExampleStateless extends StatelessWidget {
final String userName;
final int points;
const ExampleStateless({
#required this.userName,
#required this.points,
});
#override
Widget build(BuildContext context){
print(userName); print(points);
return Something();
}
}
Note that for the stateful widget, there are two classes and each class has its scope. The superclass ExampleStateful can share its scope to its subclass _ExampleStatefulState via the widget
class ExampleStateful extends StatefulWidget {
final String userName;
final int points;
Static final string id = "exampleState";
const ExampleStatefull({
#required this.userName,
#required this.points,
});
// scope 1
#override
_ExampleStatefulState createState() => _ExampleStatefulState();
}
class _ExampleStatefulState extends State<ExampleStateful>{
// scope 2
final String userName2 = null;
#override
void initState(){
super.initState();
print(widget.userName);
print(userName2);
print(ExampleStateful.id); // you can do this only if variable is static.
}
}
What is in scope 1 can be accessed in scope 2 via the widget properties. eg print(widget.userName); instead of print(userName);