After the update of flutter_mobx: ^2.0.6+1 and mobx_codegen: ^2.0.7 the ReactionDisposer is not recognizing changes in enum
late ReactionDisposer _refreshDisposer;
Completer<dynamic> refreshCompleter = new Completer();
#override
void initState() {
super.initState();
_refreshDisposer = reaction((_) => _vendorStore.serviceState, (_) {
if (_ != LoadingEnum.loading) {
refreshCompleter.complete();
refreshCompleter = Completer();
}
});
}
Observer is working fine but ReactionDisposer is not working according to change
flutter :3.0.5
Dart 2.17.6
The new flutter_mobx: ^2.0.6+1 and mobx_codegen: ^2.0.7 has no effect on the below import
import 'package:mobx/mobx.dart';
so to work the ReactionDisposer add this plugin mobx: ^2.0.7+5 in you'r dependencies else it won't recognize the change
Related
I am using a stateful widget,but keep getting red line under my setState method with this error:
The method 'setState' isn't defined for the type 'pickImage'.
My Flutter Version:
Flutter 2.0.4 • channel stable
ImagePicker Package Version:
image_picker: ^0.7.4
This is the code:
class pickImage extends StatefulWidget {
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() { //Keep getting red line here(under setState)
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}
#override
_pickImageState createState() => _pickImageState();
}
class _pickImageState extends State<pickImage> {
#override
Widget build(BuildContext context) {
return AlertDialog(
);
}
}
I have googled this issue, but still can't find an appropriate answer, that's why came here.
The setState method (see documentation) is a member of the State<T> class. Not the StatefulWidget class.
That means you can call it in classes derived from State<T> like your _pickImageState, but not in classes that are not. Like your class pickImage, where you are trying to call your function new.
You should be able to move the whole getImage method into your _pickImageState class.
You need to move getImage inside _pickImageState.
The setState method is a member of the State class. Not the StatefulWidget class.
Documentation
so I have this simple code:
import 'package:provider/provider.dart';
class DataModel with ChangeNotifier{
bool _isLoading = true;
set isLoading(bool value){
_isLoading = value;
notifyListeners();
}
get isLoading => _isLoading;
}
ChangeNotifier and notifyListeners() aren't recognized.
My dependencies:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
provider: ^4.3.3
I'm very confused as to why is it happening, this is the exact setup in the installation page (and it worked in other projects).
This project is also connected to a git lab project, I don't know if it is related.
btw, it's not like that with other keywords that are in the provider package - it perfectly recognizes ChangeNotifierProvider i.e
You are importing an incorrect package, the correct one is package:flutter/foundation.dart or alternatively package:flutter/material.dart, try with:
import 'package:flutter/material.dart';
class DataModel with ChangeNotifier{
bool _isLoading = true;
set isLoading(bool value){
_isLoading = value;
notifyListeners();
}
get isLoading => _isLoading;
}
package:provider/provider.dart is used in the files where the calls to the provider are made, not where it is defined.
See the example from the docs: https://github.com/flutter/samples/blob/master/provider_shopper/lib/models/cart.dart
A hint, in VS Code if you right click on ChangeNotifier and select Go to Definition, the definition can be traced to the foundation package.
I tried use Hive package in my application. But when I initialised in my app get error message:
The following HiveError was thrown building MyApp(dirty): Box not
found. Did you forget to call Hive.openBox()?
Her is my code:
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart' as path_provider;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocDir = await path_provider.getApplicationDocumentsDirectory();
Hive.init(appDocDir.path);
runApp(MyApp());
final box = await Hive.openBox('storage');
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final box = Hive.box('storage');
return MaterialApp(
title: 'Test App',
debugShowCheckedModeBanner: false,
home: CheckAuth(),
);
}
}
class CheckAuth extends StatefulWidget {
#override
_CheckAuthState createState() => _CheckAuthState();
}
class _CheckAuthState extends State<CheckAuth> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Text('Hive initialised!'),
);
}
}
Emulator
API: 28
Android: 9
Packages
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
hive: ^1.4.4+1
hive_flutter: ^0.3.1
http: ^0.12.2
cupertino_icons: ^1.0.0
path_provider: ^1.6.24
dev_dependencies:
flutter_test:
sdk: flutter
hive_generator: ^0.8.2
build_runner: ^1.10.11
flutter:
uses-material-design: true
Where I have any error in my code?
Why don't you use await Hive.initFlutter() instead of Hive.init()? The former is supposed to take care of proper app folder path on specific platform and put it's files there.
It is part of package:hive_flutter/hive_flutter.dart which deals with Flutter specific stuff
I set everything up as shown in the example project:
import 'package:get_it/get_it.dart';
import 'package:places/services/authService.dart';
final locator = GetIt.instance;
void setupLocator() {
locator.registerSingleton<AuthService>(AuthService());
print("registered");
}
with the call in the main file
void main() {
setupLocator();
runApp(MyApp());
}
I have some Check where the locator also correctly return my AuthService
class AuthGuardView extends StatefulWidget {
AuthGuardView({Key key}) : super(key: key);
#override
_AuthGuardViewState createState() => _AuthGuardViewState();
}
class _AuthGuardViewState extends State<AuthGuardView> {
#override
Widget build(BuildContext context) {
return ViewModelProvider<AuthGuardViewModel>.withConsumer(
viewModel: AuthGuardViewModel(),
onModelReady: (model) => model.initialise(),
builder: (context, model, child) => model.isLoggedIn
? Container(
color: Colors.white,
child: Text("Logged In"),
)
: SignUpView(),
);
}
}
class AuthGuardViewModel extends ChangeNotifier {
AuthService _authService = locator<AuthService>();
bool isLoggedIn = false;
void initialise() async {
isLoggedIn = await _authService.isLoggedIn();
notifyListeners();
}
}
If I do the exact same thing inside the ViewModel for the SignUpView I get the following error
flutter: The following assertion was thrown building SignUpView(dirty, state: _SignUpViewState#01129):
flutter: No type AuthService is registered inside GetIt.
flutter: Did you forget to pass an instance name?
flutter: (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you
flutter: forget to register it?)
flutter: 'package:get_it/get_it_impl.dart':
flutter: Failed assertion: line 248 pos 14: 'instanceFactory != null'
In the ViewModel for the AuthGuard I do successfully retrieve the auth service. I also commented out the locator code (because I thought it might be the async call or something like that) but the same error persists.
I am using get_it: ^4.0.1 but the error persists when downgrading to 3.x.x
Here the SignUpViewModel
class SignUpViewModel extends ChangeNotifier {
SignUpViewModel(){
if(locator.isRegistered<AuthService>()) {
AuthService _authService = locator<AuthService>();
}
}
var textInputFormatter = [
WhitelistingTextInputFormatter(RegExp(r'\d')),
PhoneNumberTextInputFormatter()
];
var textEditingController;
var context;
}
This happens when the class to be registered as singleton has async methods. To fix this you need to await the singleton to be fully generated before runApp() is ran.
void main() async {
/* WidgetsFlutterBinding.ensureInitialized() is required in Flutter v1.9.4+
* before using any plugins if the code is executed before runApp.
*/
WidgetsFlutterBinding.ensureInitialized();
// Configure injecction
await setupLocator();
runApp(MyApp());
}
Adding this answer, as I think it might help others!
I have faced the same issue earlier. For me, it was due to an ordering issue. So make sure to initiate/declare the dependency objects first and then instantiate/declare the dependent one.
Using the latest get_it version in pubspec.yaml ( now it is get_it: ^4.0.2 ) resolve the issue for me.
I have also faced this issue. Nothing made sense. Then I remembered, that I recently did case sensitive renaming.
I changed i.e. Services/Database/Database.dart to services/database/database.dart, but in one file, I used import with the lowercased version, while in the other, it still was the uppercased version. Making the case consistent throughout the project was the fix I needed.
I'm currently playing with flame, a small 2D game engine based on flutter. My code contains a rather strange Heisenbug: It works fine if you start it via run or debug. But if you set a breakpoint, it throws an error
Unhandled exception:
NoSuchMethodError: The getter 'cls' was called on null.
Although I don't think that this is related to flame, I was not able to reproduce the problem with flutter alone. So I'm including a minimal version of my flame-based code. It simply paints the phone-screen with a gray background color:
main.dart:
import 'package:flutter/material.dart';
import 'package:temp/game.dart';
void main(){
MyGame game = MyGame(); // error thrown here
runApp(game.widget);
}
game.dart:
import 'package:flame/game.dart';
import 'dart:ui';
class MyGame extends Game{
Size screenSize;
#override
void render(Canvas canvas) {
Rect screenRect = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
Paint screenPaint = Paint();
screenPaint.color = Color.fromARGB(255, 100, 100, 100);
canvas.drawRect(screenRect, screenPaint);
}
#override
void update(double t) {
}
#override
void resize(Size size) {
super.resize(size);
screenSize = size;
}
}
If you want to run this, you also have to add flame to your pubspec:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
flame: ^0.10.2
My bug happens everytime I set a breakpoint in the render method of MyGame and start debugging.
I can not reproduce this on flame: ^0.22.0 so you can just upgrade to that version and it should work fine.