How can i use Provider in flutter_native_splash package? - flutter

I want to get user's data from server and set State, when app is loading. so I use flutter_native_splash package and provider for app. the provider doesn't work.
stock_notifier.dart
class StockCodeNotifier extends ChangeNotifier {
final List<StockModel> _stocks = [];
String _stockCode = '';
String get stockCode => _stockCode;
void getStocks() {
List<StockModel> data = allStocks;
_stocks.clear();
_stocks.addAll(data);
_stockCode = _stocks[0].code;
notifyListeners();
}
main.dart
void main() {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
runApp(MyApp());
Provider.of<StockCodeNotifier>(context).getStocks();
FlutterNativeSplash.remove();
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider<StockCodeNotifier>(
create: (_)=>StockCodeNotifier(),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Home(),
),
);
}
}

Related

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
),
);
}
}

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(),
},
)

Debug mode banner is not removed

I am working on a Flutter app and want to hide the debug mode banner.
This is my code for main.dart:
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
//FirebaseCrashlytics.instance.crash();
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new MyApp(),
));
}
The debug mode banner is not removed, it is shown as always.
What do I need to change to get it removed?
Debug banner does not remove from main method, you just follow the example and you bind your MyApp() with the MaterialApp()
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: NextPage(),
);
}
}

I have created an app in flutter, but whenever i press the back button it redirects me back to the pages i had open instead of previous pages

I am very new to flutter. Learning the basics. But the back button is not working as it should in the app.
This my main.dart file:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.orange
),
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}

Flutter provider length was called on null

I have a list using flutter provider.But firestore data works.The problem is that provider. Error is the getter length was called on null. I searched this question in stackoverflow, i tried every answer but they did not solve my problem. Here is included code in bellow :
GameServices Class
class GamesServices {
String collection = 'games';
Firestore _firestore = Firestore.instance;
Future<List<GamesModel>> getGames() async {
_firestore.collection(collection).getDocuments().then((result) {
List<GamesModel> gameList = <GamesModel>[];
for (DocumentSnapshot game in result.documents) {
gameList.add(GamesModel.fromSnapshot(game));
}
return gameList;
});
}
Provider Class
class BoshphelmProvider with ChangeNotifier {
GamesServices _gamesServices = GamesServices();
List<GamesModel> _games = <GamesModel>[];
List<GamesModel> get games => _games;
BoshphelmProvider.initialize() {
loadGames();
}
loadGames() async {
_games = await _gamesServices.getGames();
notifyListeners();
}
}
Main Class
void main() {
setupLocator();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: BoshphelmProvider.initialize(),
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Boshphelm',
theme: ThemeData(
primaryColor: Constant.primaryColor,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
),
);
}
}
you are missing the code part where you try to get the length of something and it throws that error
without that i would guess you are trying to access games list before it finishes initializing, as you are not waiting for it
try this
void main() async {
WidgetsFlutterBinding.ensureInitialized();
BoshphelmProvider _provider = BoshphelmProvider();
await _provider.initialize()
setupLocator();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: _provider,
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Boshphelm',
theme: ThemeData(
primaryColor: Constant.primaryColor,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
),
);
}
}