flutter_fit_utils_provider 1.0.3 copy "flutter_fit_utils_provider: ^1.0.3" to clipboard
flutter_fit_utils_provider: ^1.0.3 copied to clipboard

A flutter package to easily create providers of modelable data. This package is part of the flutter_fit_utils environement.

example/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_fit_utils/flutter_fit_utils.dart';
import 'package:flutter_fit_utils_provider/flutter_fit_utils_provider.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider<UserProvider>(
            create: (_) => UserProvider(
                FirestoreService<User>("MyFirestoreCollection", User.fromModel),
                () => const User())),
      ],
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<bool>(
      stream: context.read<UserProvider>().initializationStream,
      builder: (context, snapshot) {
        if (!context.watch<UserProvider>().initialized) {
          return const CircularProgressIndicator();
        }

        // Show your UI here.
        return const SizedBox.shrink();
      },
    );
  }
}

/// Provider for [User] data.
class UserProvider extends DataProvider<User> {
  /// Creates a new [UserProvider].
  UserProvider(super.service, super.factoryFunc);

  @override
  bool isInstanceValid(User instance) {
    /// Implement your logic here...
    return instance.name != "Bob";
  }
}

/// Example user class.
class User extends Modelable {
  static const String _nameKey = "name";

  /// User's last name.
  final String name;

  /// Creates a new user.
  const User({this.name = "", super.id, super.userId});

  /// Creates an invalid user.
  const User.invalid()
      : name = "",
        super.invalid();

  /// Creates a user from a [Model].
  User.fromModel(super.model)
      : name = model.data[_nameKey].toString(),
        super.fromModel();

  @override
  User copyWith({String? id, String? userId, bool? invalid, String? name}) =>
      User(
        name: name ?? this.name,
        id: id ?? this.id,
        userId: userId ?? this.userId,
      );

  @override
  Model toModel() => Model(
        id: id,
        userId: userId,
        data: {
          _nameKey: name,
        },
      );
}
2
likes
150
pub points
44%
popularity

Publisher

unverified uploader

A flutter package to easily create providers of modelable data. This package is part of the flutter_fit_utils environement.

Homepage
Repository (GitHub)
View/report issues

Topics

#provider

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_fit_utils, provider, rxdart

More

Packages that depend on flutter_fit_utils_provider