The argument type 'GetDelegate?' can't be assigned to the parameter type 'Key?' - flutter-getx

After updating GetX to ^5.0.0-beta.51 Version. i get this issue.
Scaffold(
body: Navigator(
key:Get.nestedKey("1"), // => The argument type 'GetDelegate?' can't be assigned to the parameter type 'Key?'
initialRoute: Routes.HOME_TAB,
onGenerateRoute: Get.find<DashBoardController>().onGenerateRoute,
),
bottomNavigationBar: BottomNavBarWidget(),
),

Related

Can't I write braces in an if statement in List?

The following without braces worked fine:
import 'package:flutter/material.dart';
void main() {
runApp(const _MyApp());
}
class _MyApp extends StatelessWidget {
const _MyApp({super.key});
#override
Widget build(BuildContext context) {
const isFlag = true;
return MaterialApp(
home: Scaffold(
body: Column(
children: const [
Text(
"Demo1",
),
if (isFlag)
Text(
"Demo true",
)
else
Text(
"Demo flase",
)
],
),
),
);
}
}
I prefer to add braces even if there is only one expression.
I did the following and it resulted in an error.
The code that causes an error:
import 'package:flutter/material.dart';
void main() {
runApp(const _MyApp());
}
class _MyApp extends StatelessWidget {
const _MyApp({super.key});
#override
Widget build(BuildContext context) {
const isFlag = true;
return MaterialApp(
home: Scaffold(
body: Column(
children: const [
Text(
"Demo1",
),
if (isFlag) {
Text(
"Demo true",
)
} else {
Text(
"Demo flase",
)
}
],
),
),
);
}
}
Error:
lib/main.dart:21:25: Error: A value of type 'Set<Text>' can't be assigned to a variable of type 'Widget'.
- 'Set' is from 'dart:core'.
- 'Text' is from 'package:flutter/src/widgets/text.dart'
('../../../flutter-command/flutter/packages/flutter/lib/src/widgets/text.dart').
- 'Widget' is from 'package:flutter/src/widgets/framework.dart'
('../../../flutter-command/flutter/packages/flutter/lib/src/widgets/framework.dart').
if (isFlag) {
^
lib/main.dart:25:20: Error: A value of type 'Set<Text>' can't be assigned to a variable of type 'Widget'.
- 'Set' is from 'dart:core'.
- 'Text' is from 'package:flutter/src/widgets/text.dart'
('../../../flutter-command/flutter/packages/flutter/lib/src/widgets/text.dart').
- 'Widget' is from 'package:flutter/src/widgets/framework.dart'
('../../../flutter-command/flutter/packages/flutter/lib/src/widgets/framework.dart').
} else {
Can't I write braces in an if statement in List?
Referred to the following:
How to use conditional statement within child attribute of a Flutter Widget (Center Widget)
PS:
The question is only about whether or not braces can be applied.
I am asking this question because I am interested in Dart syntax.
You are creating a Set using the curly braces to read more about SET visit set literal. Your build function should be like this,
return MaterialApp(
home: Scaffold(
body: Column(
children: const [
Text(
"Demo1",
),
if (isFlag)
Text(
"Demo true",
)
else
Text(
"Demo flase",
)
],
),
),
);
Or you can use the ternary operator, Example code
return MaterialApp(
home: Scaffold(
body: Column(
children: const [
Text(
"Demo1",
),
isFlag ? Text("Demo true",) : Text("Demo flase")
],
),
),
);
Do it like this with ternary operator
isFlag?Text("Demo true"):Text("Demo flase")
Using If statements inside the UI page is Not recommended,
You Can Do What you want in tow way:
1- Using Visibility Widget, to Hide One Widget and show another one.
return MaterialApp(
home: Scaffold(
body: Column(
children: const [
Text(
"Demo1",
),
Visibility(
visible : isFlag,
child: Text(
"Demo true",
),
)
Visibility(
visible : !(isFlag),
child: Text(
"Demo false",
),
)
],
),
),
);
2- You Can Use Ternary if statement Like this:
return MaterialApp(
home: Scaffold(
body: Column(
children: const [
Text(
"Demo1",
),
isFlag ? Text("Demo true",) : Text("Demo false"),
],
),
),
);
And as I say, using a lot of code inside UI page is note recommended.
Don't be shy to ask me any thing if any.
[
if(kDebugMode){
Text('Data')
}.first,
]
{} is making it a Set so you can access its Data like here to {}.first gets first object/value from set,
in your case it is treated as a set with undefined number Objects
.
[
if (!kDebugMode) ...{
Text('Data'),
Text('data'),
},
]
or, you can also use the spread operator (...) for widgets that require the same if condition

The element type 'String' can't be assigned to the list type 'Widget'

I'm new to flutter, would like to do a List to display the image and title in Grid View Builder however I met an error. I also have to call out the list however not sure what's the issue for the error which I'm don't know how to fix. Error coming out on the code with "appItem.items[i].appImage". Much appreciate your help!
List<AppProfile> get items {
return [..._items];
}
My array(still have quite a lot however only take these two for example)
List<AppProfile> _items = [
AppProfile(
appID: "",
title: "Whatsapp",
appImage: "assets/images/Materials-03.png",
appLink: "",
),
AppProfile(
appID: "",
title: "Wechat",
appImage: "assets/images/Materials-04.png",
appLink: "",
),
]
in my builder/ display file
Widget build(BuildContext context) {
final appItem = Provider.of<AppLogic>(context, listen: false);
return Scaffold(
appBar: AppBar(
title: const Text("Your Social Media"),
),
body:
Container(
child: GridView.builder(
padding: EdgeInsets.all(30),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: 2 / 2,
crossAxisSpacing: 30, //spacing between column
mainAxisSpacing: 20,
),
itemCount: appItem.items.length,
itemBuilder: (ctx, i) {
return GestureDetector(
onTap: () {},
child: Column(
children: <Widget> [
**appItem.items[i].appImage**,
]
),
);
},
),
),
);
Error occurs
Error: Could not find the correct Provider above this SocialProfileScreen Widget
This happens because you used a BuildContext that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your main.dart and performed a hot-reload.
To fix, perform a hot-restart.
The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
You used a BuildContext that is an ancestor of the provider you are trying to read.
Make sure that SocialProfileScreen is under your MultiProvider/Provider.
This usually happens when you are creating a provider and trying to read it immediately.
For example, instead of:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// Will throw a ProviderNotFoundError, because `context` is associated
// to the widget that is the parent of `Provider<Example>`
child: Text(context.watch<Example>()),
),
}
However in my home page have floating button
Widget build(BuildContext context) {
return Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
// "Navigator.push" returns a Future and "onPressed" accepts void
//so cannot directly use "Navigator" but if using () =>
//simply means that executing "Navigator..."
onPressed: () =>
Navigator.of(context).pushNamed(SocialProfileScreen.routeName),
),
The error occurs because your appItem.items[i].appImage returns a String instead of a Widget. In this case, you need to wrap it with a Widget that displays an image.
However, you can't use a String, double, bool or other variables into a list which is the Widgets list.
So you should add an Image.assets() widget or another one (which displays images), and give your image path.
Like:
Image.asset(appItem.items[i].appImage);

What does MaterialPageRoute with the context?

What does MaterialPageRoute do with context?
And what is the purpose of builder: here.
MaterialPageRoute(builder: (context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
Every state of widget required some context to perform some work, direct or indirect context should be used.
The [BuildContext] for a particular widget can change location over time as the widget is moved around the tree. Because of this, values returned from
the methods in this class should not be cached beyond the execution of a
single synchronous function.
Example:
In the question code for back navigation used Navigator.pop(context); so it will be running in a separate state, not the build(contex) from where navigation starts

Flutter multiple `MaterialApp` lead to conflict

two MaterialApp lead to navigator occur error, because I can only modify outermost layer MaterialApp , I remove outermost layer MaterialApp, it tell me 'MediaQuery.of() called with a context that does not contain a MediaQuery.' What can I do ?
return MaterialApp(
navigatorKey: navigatorKey,
home: Scaffold(
body: Stack(
alignment: Alignment.center,
children: [
RepaintBoundary(
child: child,
key: rootKey,
),
_ContentPage()
]
)
)
);

Navigator and Provider conflict in flutter

sending data between screen using Provider with Navigator concept make a conflict
error after the run
The following ProviderNotFoundError was thrown building SecondRoute(dirty):
Error: Could not find the correct Provider above this SecondRoute Widget
To fix, please:
Ensure the Provider is an ancestor to this SecondRoute Widget
Provide types to Provider
Provide types to Consumer
Provide types to Provider.of()
Always use package imports. Ex: `import 'package:my_app/my_code.dart';
Ensure the correct context is being used.
https://www.ideone.com/xHXK5m
you can pass data like this put data in class
RaisedButton(
child: Text(‘Send data to the second page’),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(
data: data,
)),
);
},
),
and recieve data like this
class SecondPage extends StatelessWidget {
final Data data;
SecondPage({this.data});
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘Constructor — second page’),
),
body: Container(
padding: EdgeInsets.all(12.0),
alignment: Alignment.center,
child: Column(
children: <Widget>[
Container(
height: 54.0,
padding: EdgeInsets.all(12.0),
child: Text(‘Data passed to this page:’,
style: TextStyle(fontWeight: FontWeight.w700))),
Text(‘Text: ${data.text}’),
Text(‘Counter: ${data.counter}’),
Text(‘Date: ${data.dateTime}’),
],
),
),
);
}
You can try placing whatever Provider value you have above the MaterialApp and thus the navigation or, when you push to a second page, provide the value again. Providers are scoped to widget trees, so this is expected behavior.
As an example, to pass a value to another route, you could do something like
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
final foo = 'foo';
Provider<String>.value(
value: foo,
child: NewPage(),
);
}));
},
And then just consume it like regular in the NewPage route.
Try this code:
MaterialApp(
title: 'Navigation Basics',
home: ChangeNotifierProvider(
builder: (context) => Data(),
child: FirstRoute(),
))
Delete ChangeNotifierProvider in FirstRoute