Getx controller not found even after Get.put() | Flutter | Getx - flutter

"YouController" not found. You need to call "Get.put(YouController())" or
"Get.lazyPut(()=>YouController())"
Getting this error even after I have Get.put() before coming to the screen.
Any Idea?

You can try this:
create a file named initial_bindings.dart in your lib folder. Then add this snippet to it.
import 'package:get/get.dart';
class InitialBinding implements Bindings {
#override
void dependencies() {
Get.lazyPut<YouController>(() => YouController(),fenix: true);
}
}
Then in your main.dart, add this line into your GetMaterialApp.
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return GetMaterialApp(
initialBinding: InitialBinding(), // add this line
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
Then whenever you want to access the controller, you can just use Get.find<YourController>() to use it.

Related

Why MyApp and MaterialApp are underlined? The code is from initial flutter demo project

Started my new project - got always MaterialApp underlined. Requires bottomNavigationBar. I tried it differently: started a demo project in flutter - MyApp and MaterialApp are underlined as well. So i cannot run the project.
Here is the initial code of demo project:
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),);
}
}```
Please, tell me, what it wants? It is a code , made by flutter, i mean...
MyApp MyApp({Key? key})
package:project/main.dart
**2 positional argument(s) expected, but 1 found.
Try adding the missing arguments.**
Thank you
have you imported material app? your code should look like:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: MyTheme.appTheme,
debugShowCheckedModeBanner: false,
home: const HomeScreen(),
);
}
}

I'm getting this error when I add theme data in flutter

import 'package:flutter/material.dart';
import 'package:my_application_1/pages/home_page.dart';
void main() {
runApp(my_application_1());
}
class my_application_1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: home_page()
theme: ThemeData(
primarySwatch: Colors.brown
),
);
}
}
ERROR SHOWING IN THEME - Expected to find ','.dart(expected_token)
You forgot a ',' after home: homePage().
Just add it.
import 'package:flutter/material.dart';
import 'package:my_application_1/pages/home_page.dart';
void main() {
runApp(my_application_1());
}
class my_application_1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: home_page(),
theme: ThemeData(
primarySwatch: Colors.brown
),
);
}
}

Simple code Ended with this error: Gradle task assembleDebug failed with exit code 1

Just started flutter and ended up with this error. i made a signin.dart and run it through main.dart then i run the program the terminal endded up with this. here is my code:
import 'package:flutter/material.dart';
import 'package:flutters/view/signin.dart';
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override Widget build(BuildContext context) {
return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: SignIn(), );
} }
your problem is due to having main function in your widget.
in each application, you only have to have one single main function. after that in each widget you can have stateless or stateful widgets that are children of your main widget.
for example use this instead:
import 'package:flutter/material.dart';
import 'package:flutters/view/signin.dart';
class SimpleWidget extends StatelessWidget {
// This widget is the root of your application.
#override Widget build(BuildContext context) {
return Center(child:Text("Just A test"));
}
then you can use SimpleWidget in you main.dart

Provider Error: Could not find the correct Provider<Color> above this MyApp Widget

I do not understand why the following code generates the error?
According to me the provider is an ancestor to MaterialApp and it should work? What am I missing?
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
new Provider<Color>(create: (_) => Colors.amber)
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Provider.of<Color>(context),
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
)
);
}
}
Provider initially in build method. You don't access to use provider value because provider inits context, but you tried to use provider.of(context) in the same place. If you use another state(view), you should use it.
Example code : https://gist.github.com/VB10/358dc944e5a86b58db2d898eaa1b5ea6

Recommended Dark/Light or custom Theme implementation in Flutter Cupertino

I’m currently implementing an iOS style app in flutter using the CupertinoApp class and Cupertino widgets. Now I would like to implement a different Theme for the application (like feedly did), so that the user can switch between both during runtime.
Unlike a MaterialApp, CupertinoApp has no property to set the theme.
MaterialApp with theme property:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
CupertinoApp without theme property:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return CupertinoApp(
title: 'Flutter Demo',
// theme: ThemeData(
// primarySwatch: Colors.blue,
// ),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
It looks like sb. is working on a CupertinoTheme which actually seems to be a Theme for a MaterialApp and not for a CupertinoApp.
What's the recommended way to define themes and change them during runtime in a Cupertino style app (e.g. Dark and Light Theme)?
First of all CupertinoThemeData is an alternative for ThemeData:
import 'package:flutter/cupertino.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CupertinoApp(
title: 'Flutter Demo',
theme: CupertinoThemeData(),
home: MyHomePage(),
);
}
}
CupertinoThemeData has a 'raw' property which contains these properties:
Brightness, primaryColor, primaryContrastingColor, textTheme and so on.
Would you want to choose either dark or light? that's too easy!!
CupertinoThemeData.raw(Brightness.light)
CupertinoThemeData.raw(Brightness.dark)
but if you want to change it during runtime you should write a function:
CupertinoThemeData generalTheme(Brightness brightness) {
CupertinoThemeData _basicCupertinoTheme = CupertinoThemeData.raw(
brightness,
...
}
As you see, you can send an argument to function but don't forget to use setState method.
For more info check out CupertinoThemeData.raw constructor
you can use the following package to control dark and light themes, also the theme is saved directly into shared preferences so when you reopen the application, the latest theme you chose will be loaded,use the package as provided in the read me section.
https://pub.dartlang.org/packages/dynamic_theme