rust_in_flutter 1.1.0 copy "rust_in_flutter: ^1.1.0" to clipboard
rust_in_flutter: ^1.1.0 copied to clipboard

discontinuedreplaced by: rinf

Easily integrate Rust to make your Flutter app blazingly fast!

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:rust_in_flutter/rust_in_flutter.dart';
import 'package:msgpack_dart/msgpack_dart.dart';

void main() async {
  await RustInFlutter.ensureInitialized();
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ChangeNotifierProvider(
        create: (_) => HomeNotifier(),
        child: const Home(),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // `StreamBuilder` listens to a stream
            // and rebuilds the widget accordingly.
            StreamBuilder<RustSignal>(
              // Receive signals from Rust
              // with `rustBroadcaster` from `bridge/wrapper.dart`,
              // For better performance, filter signals
              // by checking the `address` field with the `where` method.
              // This approach allows the builder to rebuild its widget
              // only when there are signals
              // with the specific address it is interested in.
              stream: rustBroadcaster.stream.where((rustSignal) {
                return rustSignal.address == 'sampleCategory.mandelbrot';
              }),
              builder: (context, snapshot) {
                // If the app has just started and widget was built
                // not owing to the native signal,
                // the snapshot's data will be null.
                var received = snapshot.data;
                if (received == null) {
                  return Container(
                    margin: const EdgeInsets.all(20),
                    width: 256,
                    height: 256,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(24.0),
                      color: Colors.black,
                    ),
                  );
                } else {
                  var imageData = received.bytes;
                  return Container(
                    margin: const EdgeInsets.all(20),
                    width: 256,
                    height: 256,
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(24.0),
                      child: FittedBox(
                        fit: BoxFit.contain,
                        child: Image.memory(
                          imageData,
                          width: 64,
                          height: 64,
                          gaplessPlayback: true,
                        ),
                      ),
                    ),
                  );
                }
              },
            ),
            Consumer<HomeNotifier>(
              builder: (context, notifier, child) {
                var currentCount = notifier.count;
                if (currentCount == 0) {
                  return const Text("Not calculated yet");
                } else {
                  return Text(
                    "Current value is ${currentCount.toString()}",
                  );
                }
              },
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          context.read<HomeNotifier>().increment();
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

class HomeNotifier extends ChangeNotifier {
  int _count = 0;
  int get count => _count;
  void increment() async {
    var rustRequest = RustRequest(
      address: 'basicCategory.counterNumber',
      operation: Operation.Read,
      // Use the `serialize` function
      // provided by `msgpack_dart` package
      // to convert the message into bytes.
      bytes: serialize(
        {
          'letter': 'Hello from Dart!',
          'before_number': _count,
          'dummy_one': 1,
          'dummy_two': 2,
          'dummy_three': [3, 4, 5]
        },
      ),
    );
    // Use `sendRustRequest` from `bridge/wrapper.dart`
    // to send the request to Rust and get the response.
    var rustResponse = await requestToRust(rustRequest);
    if (!rustResponse.successful) {
      return;
    }
    // You have to explicitly tell the deserialized type
    // with `as` keyword for proper type checking.
    var message = deserialize(rustResponse.bytes) as Map;
    var innerValue = message['after_number'] as int;
    _count = innerValue;
    notifyListeners();
  }
}
45
likes
0
pub points
0%
popularity

Publisher

verified publishercunarist.com

Easily integrate Rust to make your Flutter app blazingly fast!

Repository (GitHub)
View/report issues

Funding

Consider supporting this project:

www.buymeacoffee.com

License

unknown (license)

Dependencies

ffi, flutter, flutter_rust_bridge, meta, package_config, path, plugin_platform_interface, uuid

More

Packages that depend on rust_in_flutter