realm-dart
realm-dart copied to clipboard
Ignore Realm from Web.
Problem
So I have one application that needs to be built on Native Platforms and Web. Moreover, When try to build web project the realm will break the whole web support. Is there any way that I can ignore realm binaries from the web build? I know its not supported on WEB but is there any fallback that we can use to ignore ?
TODO :
- Ignore realm.dart on web
I tried to remove this by Stackoverflow this method but doesn't seem to work.
Please help. Thanks.
Screenshot
Solution
No response
Alternatives
No response
How important is this improvement for you?
Dealbreaker
Feature would mainly be used with
Local Database only
Please
+1
I also need this, thanks
We all need this.
I haven't been able to find an easy way to achieve this. If you folks have any suggestions, we'd be more than willing to take pull requests.
I have a similar problem. I need Realm to migrate data from my legacy Android App to the new Flutter App. Since the Flutter App also supports Web as a platform, I would need to have web support for Realm too (although I won't use it, it should just compile)
We have the same problem. +1 for this. If Realm is not compatible for a target OS/platform (i.e. web in this case) Realm should not block compiling... Flutter is a multi OS/platform framework, but it appears with Realm its not. We've raise with our MongoDB AE.
The typical trick to use is:
import 'facade.dart'
if (dart.library.html) 'facade_html.dart' // don't use realm here
if (dart.library.io) 'facade_io.dart'; // here you can use realm
Where facade.dart
could be:
// No realm here!
abstract class FacadeInterface {
String get name;
}
class Facade implements FacadeInterface {
@override
String get name => 'Stub'; // will never be called
}
facade_io.dart
where we can use realm
:
import 'package:realm/realm.dart';
import 'facade.dart' as facade;
part 'facade_io.g.dart';
@RealmModel()
class _Stuff {
late String name;
}
final realm = () {
final result = Realm(Configuration.local([Stuff.schema]));
if (result.all<Stuff>().isEmpty) {
result.write(() => result.add(Stuff('Realm')));
}
return result;
}();
class Facade implements facade.FacadeInterface {
@override
String get name => realm.all<Stuff>().first.name;
}
and for web, facade_html.dart
:
import 'facade.dart' as facade;
// no realm here
class Facade implements facade.FacadeInterface {
@override
String get name => 'Web';
}
and a sample main.dart
could be:
import 'package:flutter/material.dart';
import 'facade.dart'
if (dart.library.html) 'facade_html.dart'
if (dart.library.io) 'facade_io.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Material(
child: Center(
child: Text(
Facade().name,
textScaler: const TextScaler.linear(5),
),
),
),
);
}
}
Result:
But since you ask, I suspect this is not sufficient for you. Could you elaborate a bit on what you hope for?
Hi All,
@nirinchev , @richard457 , @shockbytes , @panalgin , @nielsenko , @dotjon0 @
So this has worked for me from like the time when I raised this thing. After spending many hours, I found that the trick I am using is working and hence being used in my live code. This won't work if all your code is dumped in a single file. This is a bit of hustle but once you are on to it can be easily used. 💯 I hope this works. 😀
- I am currently using CLEAN hence was easy to manage.
Let's Say you have a class called UserInfoRepository
that has all the methods for io and web.
IO devices includes => iOS , Android, Windows
Web => will be different.
Create UserInfoRepository as abstract and then create 2 classes called UserRepositoryWeb
, UserRepositoryIO
abstract class UserRepository {
void getUser();
}
class UserRepositoryWeb extends UserRepository{
/// You can only Remote datasources
@override
void getUser(){
/// Get and work only on remote
}
}
class UserRepositoryIo extends UserRepository{
// you can include Local Datasource & remote datasources
@override
void getUser(){
// get and work in/from local/remote
}
}
The most important part now is that you separate the files using
- Folder structure
- Dependency Injection
Folder Structure
Dependency Injection
I was using GetIt hence let me be specific about GetIt
Create 2 files
- InjectionWeb.dart
- InjectionIo.dart
and use something like this
Global Declaration
final locator = GetIt.instace;
IO Injection
void setupIoLocator() {
locator.registerSingleton<UserRepository>(() => UserRepositoryIo());
}
Web Injection
void setupWebLocator() {
locator.registerSingleton<UserRepository>(() => UserRepositoryWeb());
}
Important Notes
- Make sure you do not include the
realm.dart
anywhere outside these IO files - While running on web make sure never from the whole code base the io repos , local data sources are exposed to web files
- Maintain Separate files if mixed then I am sure the code will be down and won't compile.
Thanks for this @rdunlocked18, unfortunately this approach will not work for us as our Realm Models are used throughout the application left right and centre. I suppose for more simple use cases of Realm the above looks like a good option.
@dotjon0 how do you expect to use the Realm models in a web environment? Are you interested in just having the unmanaged objects in the web and not touch and of the database code?
Hey @nirinchev hope you are well?
Unmanaged objects would be amazing, and yes we would just call different functions for web (using our API instead of MongoDB Device Sync). So for example we could define a RealmObject (Cats) and populate this collection via:
- MongoDB Device Sync for our native apps (windows/macOS)
- Our API for our web app (i.e. using the unmanaged 'Cats' RealmObject)
Of course in an ideal world we would be using MongoDB Device Sync with a persistent local Realm DB in web...but this is probably for later and when browsers mature some more!
Just checking in to see if there has been any progress on this?
@dotjon0 It is not top priority, but we have been discussing this. We cannot give any timeline.
@dotjon0 It is not top priority, but we have been discussing this. We cannot give any timeline.
appreciate the update @nielsenko and hope you are well