flutter_kore 1.4.5 copy "flutter_kore: ^1.4.5" to clipboard
flutter_kore: ^1.4.5 copied to clipboard

This library contains components for Flutter architecture, and also utility classes for HTTP, navigation and DI

flutter_kore package #

header

Code Coverage

Set of classes for Flutter app architecture.

Installing #

dependencies: 
  flutter_kore: ^<latest version>

dev_dependencies: 
  flutter_kore_generator: ^<latest version>

You also need dependency for build_runner if you don't have it yet.

dev_dependencies: 
  build: ^<latest version>
  build_config: ^<latest version>
  build_runner: ^<latest version>

To build/rebuild generated files use:

flutter pub run build_runner build --delete-conflicting-outputs

Examples #

Minimal example can be found here.

More complex examples can be found here.

Template project can be found here.

There are small example with all basic components, example of using navigation and example of connecting database.

Docs #

Here is small example demonstrating all components:

import 'package:dart_mappable/dart_mappable.dart';
import 'package:dio/dio.dart' as dio;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_kore/flutter_kore.dart';
import 'package:flutter_kore/flutter_kore_widgets.dart';

part 'main.api.dart';
part 'main.kore.dart';
part 'main.mapper.dart';

class PostLikedEvent {
  final int id;

  const PostLikedEvent({
    required this.id,
  });
}

@MappableClass()
class Post with PostMappable {
  const Post({
    required this.title,
    required this.body,
    required this.id,
    this.isLiked = false,
  });

  final String? title;
  final String? body;
  final int? id;
  final bool isLiked;

  static const fromMap = PostMapper.fromMap;
}

@mainApi
class Apis with ApisGen {}

@mainApp
class App extends KoreApp with AppGen {
  final apis = Apis();

  @override
  Future<void> initialize() async {
    await super.initialize();
  }
}

final app = App();

Future<void> main() async {
  await app.initialize();

  runApp(const MaterialApp(home: PostsListView()));
}

class HttpRequest<T> extends DioRequest<T> {
  @override
  RequestSettings<dio.Interceptor> get defaultSettings => RequestSettings(
        logPrint: (message) {
          print(message);
        },
        exceptionPrint: (error, trace) {
          print(error);
          print(trace);
        },
      );
}

@api
class PostsApi {
  HttpRequest<List<Post>> getPosts(int offset, int limit) =>
      HttpRequest<List<Post>>()
        ..method = RequestMethod.get
        ..baseUrl = 'http://jsonplaceholder.typicode.com'
        ..url = '/posts'
        ..parser = (result, headers) async {
          final list = <Post>[];

          result?.forEach((data) {
            list.add(Post.fromMap(data));
          });

          return list;
        };
}

@MappableClass()
class PostsState with PostsStateMappable {
  const PostsState({
    this.posts,
  });

  final StatefulData<List<Post>>? posts;
}

@basicInstance
class PostsInteractor extends BaseInteractor<PostsState, Map<String, dynamic>> {
  Future<void> loadPosts(int offset, int limit, {bool refresh = false}) async {
    updateState(state.copyWith(posts: const LoadingData()));

    late Response<List<Post>> response;

    if (refresh) {
      response = await executeAndCancelOnDispose(app.apis.posts.getPosts(0, limit));
    } else {
      response = await executeAndCancelOnDispose(app.apis.posts.getPosts(offset, limit));
    }

    if (response.isSuccessful) {
      updateState(state.copyWith(posts: SuccessData(result: response.result ?? [])));
    } else {
      updateState(state.copyWith(posts: ErrorData(error: response.error)));
    }
  }

  @override
  List<EventBusSubscriber> subscribe() => [
        on<PostLikedEvent>((event) {
          // update posts list...
        }),
      ];

  @override
  PostsState get initialState => const PostsState();
}

class PostsListView extends StatefulWidget {
  const PostsListView({
    super.key,
  });

  @override
  State<StatefulWidget> createState() {
    return _PostsListViewWidgetState();
  }
}

class _PostsListViewWidgetState extends BaseIndependentView<PostsListView> {
  @override
  DependentKoreInstanceConfiguration get configuration =>
      DependentKoreInstanceConfiguration(
        dependencies: [
          app.connectors.postsInteractorConnector(),
        ],
      );

  late final postsInteractor = useLocalInstance<PostsInteractor>();

  void openPost(Post post) {
    // read more about navigation component in docs

    // app.navigation.routeTo(
    //   app.navigation.routes.post(
    //     post: post,
    //   ),
    //   forceGlobal: true,
    // );
  }

  void like(int id) {
    app.eventBus.send(PostLikedEvent(id: id));
  }

  @override
  Widget buildView(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 232, 232, 232),
      appBar: AppBar(title: const Text('Posts')),
      body: KoreStreamBuilder<StatefulData<List<Post>>?>(
        streamWrap: postsInteractor.wrapUpdates((state) => state.posts),
        builder: (context, snapshot) {
          // build list with snapshot.data...
        },
      ),
    );
  }
}

Learn about components:

layers main

Global layer

Data Layer

Domain Layer

Presentation layer

Utility

Other materials

Important note

To generate test coverage report run sh coverage.sh.

VSCode extension

To generate common folders like view folders, interactors and wrappers you can use VSCode extension

1
likes
160
points
187
downloads

Publisher

verified publisherredcollar.co

Weekly Downloads

This library contains components for Flutter architecture, and also utility classes for HTTP, navigation and DI

Repository (GitHub)
View/report issues

Topics

#state-management #di #navigation #http

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

async, dio, flutter, universal_platform, visibility_detector

More

Packages that depend on flutter_kore