Skip to content

Contoh penggunaan parameter pada state cubit


Berikut contoh kode dengan penggunaan dengan Freezed, Build Runner, Get It, Cubit Bloc, dan Singleton.

File: my_cubit.dart

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'my_state.freezed.dart';

class MyCubit extends Cubit<MyState> {
  MyCubit() : super(const MyState.initial());

  void getData(String param) {
    emit(const MyState.loading());

    // Simulasikan proses pengambilan data
    Future.delayed(const Duration(seconds: 2), () {
      // Simulasikan skenario sukses
      emit(MyState.loaded('Data dari $param')); 

      // Simulasikan skenario error
      // emit(MyState.error('Gagal mengambil data')); 
    });
  }
}

File: my_state.dart

part of 'my_cubit.dart';

@freezed
class MyState with _$MyState {
  const factory MyState.initial() = Initial;
  const factory MyState.loading() = Loading;
  const factory MyState.loaded(String data) = Loaded;
  const factory MyState.error(String message) = Error;
}

File: main.dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'my_cubit.dart';

void main() {
  configureDependencies();
  runApp(MyApp());
}

void configureDependencies() {
  GetIt.I.registerSingleton<MyCubit>(MyCubit());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Contoh Cubit dengan Parameter',
      home: BlocProvider(
        create: (context) => GetIt.I.get<MyCubit>(),
        child: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController _paramController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Contoh Cubit dengan Parameter'),
      ),
      body: BlocBuilder<MyCubit, MyState>(
        builder: (context, state) {
          return _buildBody(state);
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          context.read<MyCubit>().getData(_paramController.text);
        },
        child: Icon(Icons.send),
      ),
    );
  }

  Widget _buildBody(MyState state) {
    switch (state) {
      case Initial:
        return Center(child: Text('Masukkan parameter'));
      case Loading:
        return Center(child: CircularProgressIndicator());
      case Loaded loaded:
        return Center(child: Text(loaded.data));
      case Error error:
        return Center(child: Text(error.message));
      default:
        return Container();
    }
  }
}

File: get_it.dart

import 'package:get_it/get_it.dart';

void configureDependencies() {
  GetIt.I.registerSingleton<MyCubit>(MyCubit());
}

Penjelasan penggunaan dependency:

  • Freezed: Digunakan untuk menghasilkan boilerplate code dan membuat konstruktor state lebih mudah.
  • Build Runner: Digunakan untuk menjalankan generator code Freezed.
  • Get It: Digunakan untuk dependency injection dan menyediakan singleton instance MyCubit.
  • Singleton: MyCubit dibuat sebagai singleton untuk memastikan hanya ada satu instance yang digunakan di seluruh aplikasi.

Leave a Reply

Your email address will not be published. Required fields are marked *