Why can't I use Getx package services? - flutter-getx

enter image description herei added the getx package to my pubspec.yaml file and import the 'get/get.dart' in main.dart but when i want to use getPages, i cant and it gives an error
I don't know what to try because I don't know much about getx package

You should use GetMaterialApp widget instead of MaterialApp since its the widget from get package
Example:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'My App',
theme: ThemeData(
scaffoldBackgroundColor: const Color(0xff262626),
brightness: Brightness.dark,
),
initialRoute: '/',
getPages: [
GetPage(name: '/', page: () => HomePage()),
],
);
}
}

Related

could not find a generator for route RouteSettings("org_Item", null) in the _WidgetsAppState

I´m newbie to flutter and reveice one exception about route, I don't why I am getting this error in the debug console 'could not find a generator for route RouteSettings("org_Item", null) in the _WidgetsAppState.'
provinces_item.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:let_us_volunteer/screens/orgscreen.dart';
class provwidget extends StatelessWidget {
final String id;
final String title;
final String image;
provwidget(this.id, this.title, this.image);
void selectOrg(BuildContext ctx) {
Navigator.of(ctx).pushNamed(orgscreen.routeName);
}
#override
Widget build(BuildContext context) {
return InkWell(
onTap: () => selectOrg(context),
child: Image.asset(image),
);
}
}
main.dart
import 'package:flutter/material.dart';
import 'package:let_us_volunteer/screens/log_in.dart';
import 'screens/page_view.dart';
import 'screens/log_in.dart';
import 'screens/orgscreen.dart';
void main() async {
runApp(page_view());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter demo',
theme: ThemeData(
primarySwatch: Colors.purple,
primaryColor: Colors.purple,
),
home: const log_in_screen(),
routes: {
'/': (context) => log_in_screen(),
orgscreen.routeName: (context) => orgscreen(),
},
);
}
}
orgscreen.dart
import 'package:flutter/material.dart';
class orgscreen extends StatefulWidget {
static const routeName = 'org_Item';
#override
State<orgscreen> createState() => _orgscreenState();
}
class _orgscreenState extends State<orgscreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('ggg')),
body: null,
);
}
}
If someone knows the solution it would be grateful for help, and explaining what was wrong, thanks
Try to remove your home: const log_in_screen(), and for format case use '/org_Item';
title: 'flutter demo',
theme: ThemeData(
primarySwatch: Colors.purple,
primaryColor: Colors.purple,
),
routes: {
'/': (context) => log_in_screen(),
orgscreen.routeName: (context) => orgscreen(),
},
And not sure about your page_view, you can check
runApp(MyApp());
The correct way of define route is use / with it, So change your route to this:
static const routeName = '/org_Item';
when yo do not run your main class (MyApp) in your runApp, it can not define that route, so also correct this:
runApp(MyApp());
last thing when you define / this in your routes, you should not use home property of MaterialApp, so remove home: const log_in_screen(), and your final MaterialApp should look like this:
MaterialApp(
title: 'flutter demo',
theme: ThemeData(
primarySwatch: Colors.purple,
primaryColor: Colors.purple,
),
routes: {
'/': (context) => log_in_screen(),
orgscreen.routeName: (context) => orgscreen(),
},
)

status bar overlapping my WebView screen, how to fix this?

I am building APK for my web app but the upper part of the web app is overlapped by the status bar
I want to show the status bar but not overlap my Webview, Webview must use screen size except status bar
check screenshot of the problem
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(const myapp());
}
// ignore: camel_case_types
class myapp extends StatelessWidget {
const myapp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "Sample App",
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const Scaffold(
appBar: null ,
body: WebView(
initialUrl: 'https://primexop.com/motap/appw1/index.php?app_v=1.001010&ow_id=101',
javascriptMode: JavascriptMode.unrestricted,
)),
);
}
}
wrap your Scaffold or WebView with SafeArea Widget
check the code below which fixes this issue
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(const myapp());
}
// ignore: camel_case_types
class myapp extends StatelessWidget {
const myapp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "Sample App",
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const SafeArea(
child: Scaffold(
appBar: null,
body: WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: "https://primexop.com/motap/appw1/index.php?app_v=1.001010&ow_id=101",
)),
),
);
}
}
I used SafeArea to wrap the Scaffold
Result screenshot

"GetXController" not found. You need to call?

I want to implement GetX binding methods, and as result, I got this error:
════════ Exception caught by widgets library ═══════════════════════════════════
The following message was thrown building MyApp(dirty):
"UserController" not found. You need to call "Get.put(UserController())" or "Get.lazyPut(()=>UserController())"
The relevant error-causing widget was
MyApp
lib/main.dart:9
main.dart
import 'package:donirajkrv/views/welcome.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import './router/index.dart';
import './bindings/user_binding.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GetMaterialApp(
initialBinding: UserBinding(),
home: const Scaffold(
backgroundColor: Color.fromRGBO(244, 248, 252, 1),
body: SafeArea(child: WelcomePage()),
),
getPages: [...routeList],
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: const Color(0XFFFB6394),
appBarTheme: const AppBarTheme(
systemOverlayStyle: SystemUiOverlayStyle.dark,
),
),
);
}
}
How main works:
void main() {
Get.put<UserController>(UserController());
runApp(const MyApp());
}
user_binding.dart
import 'package:get/get.dart';
import 'package:donirajkrv/controllers/user_controller.dart';
class UserBinding implements Bindings {
#override
void dependencies() {
Get.put(() => UserController());
}
}
Routelist:
import 'package:get/get.dart';
import '../views/welcome.dart';
import '../views/login.dart';
import '../views/register.dart';
import '../views/home_page.dart';
import '../middlewares/auth_middleware.dart';
import '../middlewares/back_to_home_middleware.dart';
// Routes path
import '../routes/index.dart';
List routeList = [
GetPage(
name: '/${Routes.WELCOME_PAGE}',
page: () => const WelcomePage(),
middlewares: [BackToHomeMiddleware()]),
GetPage(
name: '/${Routes.LOGIN_PAGE}',
page: () => const Login(),
middlewares: [BackToHomeMiddleware()]),
GetPage(
name: '/${Routes.REGISTER_PAGE}',
page: () => const Register(),
middlewares: [BackToHomeMiddleware()]),
GetPage(
name: '/${Routes.HOME_PAGE}',
page: () => const HomePage(),
middlewares: [AuthMiddleware()])
];
i have added the detail code for binding controller and increment function so try using this, it will definitely work
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GetMaterialApp(
initialBinding: UserBinding(),
getPages: [...routeList],
initialRoute: RouteNames.welcomePage,
home: const Scaffold(
backgroundColor: Color.fromRGBO(244, 248, 252, 1),
),
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: const Color(0XFFFB6394),
appBarTheme: const AppBarTheme(
systemOverlayStyle: SystemUiOverlayStyle.dark,
),
),
);
}
}
class UserController extends GetxController {
var i = 0.obs;
void increment(){
i++;
}
}
class RouteNames {
static String welcomePage = "/welcomePage";
}
List routeList = [
GetPage(
name: '/${RouteNames.welcomePage}',
page: () => WelcomePage(),
),
];
//here use binding like this
class UserBinding implements Bindings {
#override
void dependencies() {
Get.put<UserController>(UserController(), permanent: true);
}
}
class WelcomePage extends StatelessWidget {
WelcomePage({Key? key}) : super(key: key);
// final userController = Get.put(UserController());
final userController = Get.find<UserController>();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
),
body: Column(
children: [
Obx(() => Text(userController.i.value.toString(),
style: const TextStyle(
fontSize: 25,
color: Colors.blueAccent
),),),
ElevatedButton(onPressed: (){
userController.increment();
}, child: const Text("Button")),
],
),
);
}
}
you should make a simple change
Instead of
GetMaterialApp(
initialBinding: BindingsBuilder(() {
Get.put(UserBinding());
})...
use
initialBinding: UserBinding())...
GetMaterialApp(
initialBinding: UserBinding())...
In the user_binding.dart file you can initiate every controller that your app uses.
and then you can use it by simply declare a variable like this
UserController controller = Get.find();
//or like this:
final controller = Get.find<UserController>();
//I haven't tested the second one, but it should work.
Hope it works :)
You can also follow the example below:
https://github.com/Prosa/Flutter-GetX-Bindings-Example

Flutter ProviderScope application open error

Flutter when use riverpod in release mode application crash and when remove provider scope it is working. Widgets do not appear when the application is opened.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:grock/grock.dart';
import 'package:turesta/constant/constant.dart';
import 'package:turesta/view/normal/splash/splash.dart';
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends ConsumerWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context, WidgetRef ref) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Turesta',
navigatorKey: Grock.navigationKey,
scaffoldMessengerKey: Grock.snackbarMessengerKey,
theme: ThemeData(
scaffoldBackgroundColor: Constant.scaffoldBgColor,
),
home: Splash(),
);
}
}

I can't use onGenerateRoute, because there is always an error

I can't use onGenerateRoute because there is always an error. I watched different tutorials but I can't find the problem. GenerateRoute behind the "Route>dynamic>" is always underlined red.
This is my "route_generator.dart" file:
import 'package:flutter/material.dart';
import 'package:rave/src/app.dart';
class RouteGenerator{
static Route<dynamic> generateRoute(RouteSettings settings){
}
}
and this is my "app.dart" file, the RouteGenerator behind "onGenerateRoute:" is also underlined red:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:rave/src/Screens/init.dart';
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App',
theme: ThemeData(
primarySwatch: Colors.blue,
scaffoldBackgroundColor: const Color(0xff1f1f1f),
canvasColor: const Color(0xff1f1f1f),
),
home: InitScreen(),
onGenerateRoute: RouteGenerator.generateRoute,
);
}
}