Flutter - Isar schema is not defined - flutter

I have decided to use Isar database in my next project and I find it much helpful when dealing with local data.
I followed the quickstart guide in its website. I added dependencies. Annotated the contact class. Ran code generator. But at fourth step, I have problem creating schema while creating Isar instance.
initIsar() async {
final dir = await getApplicationSupportDirectory();
final isar = await Isar.open(
schemas: [ContactSchema],
directory: dir.path,
inspector: true,
);
}
The problem is where I typed ContactSchema, it says
Undefined name 'ContactSchema'.
Try correcting the name to one that is defined, or defining the name.
So question I have to ask is, I followed guide but I'm unable to create a schema. How can I create one to make Isar db work?
UPDATE:
import 'package:isar/isar.dart';
part 'contact.g.dart';
#Collection()
class Contact {
#Id()
int? id;
late String name;
}
After adding part 'contact.g.dart', type this command flutter pub run build_runner build and you are good to go.

For completeness of response:
Run (error? look at it, it likely say to remove isar from the first command)
flutter pub add isar isar_flutter_libs
flutter pub add -d isar_generator build_runner
flutter pub run build_runner build # Run every update of the collection
Example collection:
#Collection(accessor: "time")
class Timer {
final Id id = Isar.autoIncrement;
#Index(
unique: true,
replace: true,
)
late final bool isRunning;
late final DateTime dates = DateTime.now();
}
Suggestion: (tree -x output)
$ tree -x
.
├── main.dart
└── model
├── timer.dart
└── timer.g.dart
1 directory, 3 files
For how I'm using this:
void main() async {
// ignore: unused_local_variable
final isar = await Isar.open([TimerSchema]);
runApp(const MyApp());
}
Look at my timer app for help: pranitshah.cyou
I'll update my website for this.

After you run the build_runner command the schema class is generated in the MODEL_NAME.g.dart file. you need to import that file to get access to the schema class.

Hello Zahid If you want to add local db in your next flutter app there are various packgaes on https://pub.dev.
I would highly recomend you to go start with https://pub.dev/packages/floor it is easy to learn light weight to use..
Try this you would loved it.. and how to implement you can google it and here is a tutorial for how to use Floor in your flutter Mobile App.
https://www.youtube.com/watch?v=cQ7W7vpwTbk&t=1055s

Related

How to setup different firebase environments(Flavors) in Flutter Web

I am trying to set up different firebase environments(for example :qa,prod) in a flutter project.
I created two projects in firebase, one for production, one for the qa. Then, in an iOS or Android project, I using separate google-services.json or GoogleServices-Info.plist files.
but,how to do it in Flutter web?
If you need this dynamic config in dart code, you can create an interface that represents your configs and create implementations for each environment, so you can change the implementation passing a parameter when run flutter app (using --dart-define).
Imagine the case that you have a prod and an homolog environment, you can create three files:
The first one is the contract of your configs, that all environments must have implemented.
abstract class AppConfig {
String get baseUrl;
}
And we create a class for each environment, with the implementation of the configs defined in AppConfig.
class ProdConfig implements AppConfig {
#override
String get baseUrl => 'https://my-prod-url.com';
}
class HomologConfig implements AppConfig {
#override
String get baseUrl => 'https://my-homolog-url.com';
}
When we need to get the config class, we instantiate based on parameter passed by --dart-define
AppConfig getConfig() {
const environmentParameter = String.fromEnvironment('ENV');
switch (environmentParameter) {
case 'prod': return ProdConfig();
case 'homolog': return HomologConfig();
default: throw Error(); // You can set a default environment
}
}
And when you run your app, you just need to pass this parameter, like the example below:
flutter run --dart-define ENV=prod
Or in homolog:
flutter run --dart-define ENV=homolog
BUT, if you need to set configs like firebase, you should check this answer

How to ignore package when building flutter project for web?

I have a project which uses flutter_libserialport library on macOS.
I am modifying it to work on web however this library does not work on web.
I am building a web implementation using navigator.serial in javascript which works fine.
However when I attempt to build the project for web I get the following error
/opt/homebrew/Caskroom/flutter/2.2.3/flutter/.pub-cache/hosted/pub.dartlang.org/libserialport-0.2.0+3/lib/src/config.dart:25:8: Error: Not found: 'dart:ffi'
import 'dart:ffi' as ffi;
This makes sense since FFI is not available on web.
But I don't even need the libserialport library on web any way.
How can I get flutter to ignore it?
I tried this however it doesn't contain information on how to exclude a package.
It also does not contain information on how to ignore it specifically for web. It seems to just ignore it in general.
Maybe you should guard your usages of libserialport with the kIsWeb predicate like following:
if(!kIsWeb){
// libserialport code execution here
}
I searched a lot as well and didn't find a way you can do that, I think this should be handled by the package itself not the package's users like in path_provider for instance.
As a workaround I have created a dummy libserialport's SerialPort class for web only as follows:
dummy_serialport.dart:
class SerialPort {
final String name;
static List<String> availablePorts = ['dummy'];
static SerialPortError? lastError;
SerialPort(this.name);
bool openReadWrite() {
return false;
}
}
class SerialPortError {}
// add more properties and functions as needed
main.dart:
import 'package:libserialport/libserialport.dart'
if (dart.library.html) './dummy_serialport.dart'
if (dart.library.io) 'package:libserialport/libserialport.dart';
....
if (!kIsWeb) {
final name = SerialPort.availablePorts.first;
final port = SerialPort(name);
if (!port.openReadWrite()) {
print(SerialPort.lastError);
exit(-1);
}
}
....
....
It's bad, I know :( but it works! maybe you can contact the package author to get more insight and if opening a PR where the interfaces are separated from the FFI implementation so that importing the classes wouldn't break web or something.

Unexpected text 'await' when opening database with Flutter

I'm following the Persist data with SQLite tutorial from the Flutter Dev website, and I'm trying to open a database connection using async, but I keep getting the error Unexpected text 'await'. Even when I copy the code directly from the tutorial I'm still getting the same error.
This is my full file:
import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
// Open the database and store the reference.
final Future<Database> database = openDatabase(
join(await getDatabasesPath(), 'doggie_database.db'),
);
As far as I can tell I'm using the keyword correctly, so why is the compiler complaining?
I'm using Dart 2.7.0-dev.2.1 if that makes a difference.
This seems like an issue with asynchronicity. It's strange that an example from the official documentation is leading you into this mistake. Directly assigning a variable outside of a method that would need to be async. Try this:
final Future<Database> database = getDatabasesPath().then((String path) {
return openDatabase(join(path, 'doggie_database.db'));
});
For future reference, the reason why the OP's code wasn't working was because he was running it outside of the main() Flutter method, which is async in the example provided by the Flutter documentation: https://flutter.dev/docs/cookbook/persistence/sqlite#example

Flutter - Error: The getter X isn't defined for the class

I have a class TripController that contains a private field _updatedAccount. I created a getter in order to get from outside.
class TripController {
final String _accountId;
final BuildContext _context;
Account _updatedAccount;
Account updatedAccount() => _updatedAccount;
TripController(this._accountId, this._context);
...
}
In another class, where I perfectly have access to the TripController class, I have the code :
onTap: () {
TripController _tripController =
new TripController(_account.id, context);
_tripController.add(context);
_account.trips = _tripController.updatedAccount.trips;
_account.notifyListeners();
},
And here, updatedAccount from _tripController.updatedAccount.trips is underlined in red with the message : The getter 'updatedAccount' isn't defined for the class 'TripController'
Did I misdeclared the getter ?
Okay, I finally fixed it. I don't know why, but I had to delete the code related to TripController, and ther re-write it again. I don't know why, maybe it was an Editor problem, I'm using VScode.
You have declared updatAccount() as a method, not as a getter. Use _tripController.updatedAccount().trips; or change the method to a getter Account get updatedAccount => _updatedAccount;
I faced this error and the problem was my IDE, not my code.
This solution worked for me in Android Studio :
File -> Invalidate Catches... -> Invalidate And Restart
You are using the classic method syntax declaration, in Dart language, prefer using this kind of syntax for getters:
Account get updatedAccount => _updatedAccount;
And call it the way you did.
Else you should call it like a classic method:
_tripController.updatedAccount().trips
Please follow this link for further information:
https://dart.dev/guides/language/language-tour#getters-and-setters
If you want to use import 'package:flutter_svg/svg.dart'; package then you need to follow these steps.
step 1. -> Open the termial in the IDE and run this command -> flutter pub add flutter_svg
step 2. -> Then run this command -> flutter pub get
step 3. -> Now import this package -> import 'package:flutter_svg/flutter_svg.dart';
Try to check the model class name you use might the same with existing name of Widget or Plugin
Check your imported libraries on the top of your file where the problem is. In my case, Android Studio auto-imported an object from the core Flutter library with the same name as the model I've defined on my own.
I just deleted the import and then imported my own object from my_models.dart file.

How to modify class name when creating a Flutter plugin

I'm creating a new Flutter plugin
I named it my_flutter_plugin when I created it. But now I want to change the main class name.
Currently it is this:
// lib/my_flutter_plugin.dart
import 'dart:async';
import 'package:flutter/services.dart';
class MyFlutterPlugin {
static const MethodChannel _channel =
const MethodChannel('my_flutter_plugin');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}
I'd like to change MyFlutterPlugin to AnotherName, but when I look in pubspec.dart it says:
# The following section is specific to Flutter.
flutter:
# This section identifies this Flutter project as a plugin project.
# The androidPackage and pluginClass identifiers should not ordinarily
# be modified. They are used by the tooling to maintain consistency when
# adding or updating assets for this project.
plugin:
androidPackage: com.example.my_flutter_plugin
pluginClass: MyFlutterPlugin
I want to keep the package name as my_flutter_plugin but this seems to indicate that I can't (or shouldn't) change the plugin class name.
How do I change the class name?
The pluginClass in pubspec.yaml is not the same as your class with the method channel. In fact, if you had named your project my_flutter instead of my_flutter_plugin, the pluginClass in pubspec.yaml would still say myFlutterPlugin but your lib class with the method channel would be MyFlutter.
How to change the class name
In your lib folder class (in this case lib/my_flutter_plugin.dart), just refactor the class name as you would normally rename any class. You can rename it to AnotherName. As long as you are refactoring using the IDE tools, this should also update the class name in the example and test folders.
How to change the method channel name
Just change the string name everywhere that it is used. In a new plugin project, this would be in the lib folder and the test folder. Rather than
static const MethodChannel _channel =
const MethodChannel('my_flutter_plugin');
You can give it a unique name like
static const MethodChannel _channel =
const MethodChannel('com.example.my_flutter_plugin/another_name');