Desdaemon / flutter_rust_bridge_template

Template for Flutter + Rust integration with flutter_rust_bridge.
https://desdaemon.github.io/flutter_rust_bridge_template/
Apache License 2.0
120 stars 63 forks source link

Able run with "just serve" command, but cannot run in production #61

Closed hoangquocan closed 7 months ago

hoangquocan commented 8 months ago

Hi! I'm using this Template since FRB v1.75, it almost works Well for all my cases. When i want to deploy with Web Platform, it can not use function from Rust (in dev mode it has no problem). it also has no CORS error, only error like my Attachments. Sorry about my English not well and Issue in deployment seems little for research! Screenshot from 2023-12-31 20-20-31

Desdaemon commented 8 months ago

There could be issues with initializing the WASM module if the wire_* functions are not available. I'm also trying to set up an HTTPS server on local for troubleshooting this issue, but more details would definitely be helpful.

hoangquocan commented 8 months ago

i try firebase deploy with simple example like that: pub fn greet() -> Result<String>{ Ok("Hello world from Rust!".to_string()) }

import "package:flutter/material.dart";
import '../ffi.io.dart' if (dart.library.html) '../ffi.web.dart';
export '../ffi.io.dart' if (dart.library.html) '../ffi.web.dart' show api;

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});
  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  String _greet = "";
  @override
  void initState() {
    _callRustFfi();
    super.initState();
  }

  Future<void> _callRustFfi() async {
    final result = await api.greet();
    if (mounted) setState(() => _greet = result);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Center(
        child: Text(_greet),
      )),
    );
  }
}

and in ffi.web.dart:

// ignore_for_file: avoid_web_libraries_in_flutter

import 'dart:html';
import 'package:flutter_rust_bridge/flutter_rust_bridge.dart';
import 'bridge_generated.web.dart';

const root = 'pkg/native';

Future<WasmModule> _initModule() {
  if (crossOriginIsolated != true) {
    return Future.error(const MissingHeaderException());
  }

  final script = ScriptElement()..src = '$root.js';
  document.head!.append(script);
  return script.onLoad.first.then((_) {
    eval("window.wasm_bindgen = wasm_bindgen");
    return wasmModule.bind(wasmModule, '${root}_bg.wasm');
  });
}

final api = NativeImpl.wasm(_initModule());

i wonder if i just need to use build/web folder has compiled by just serve command then firebase deploy?

github-actions[bot] commented 8 months ago

This issue has been marked as stale because it has no activity recently. Please remove the 'stale' label, or comment if the issue persists.

github-actions[bot] commented 7 months ago

This issue has been closed after a period of inactivity. Please reopen this issue if the problem persists, or open a new one if you have a similar problem.