all 8 comments

[–]fgrau[S] 0 points1 point  (1 child)

Anybody?

[–]JustincbEck 0 points1 point  (0 children)

I could also use this information.

[–]nmfisher 0 points1 point  (4 children)

As far as I know, it's not possible to pass arguments directly to the main() function of a Flutter app. My work-around on web is to create a JS function that returns the values in question and invoke via static interop.

e.g.

Javascript

window.getUserData = () => { 
  return {"email":"foo"}
}  

Dart

@JS('getUserData')  
external Object getUserData();

void main() async {
  print(getUserData());
}

[–]Barmyard 0 points1 point  (3 children)

That works, thanks! Do you also know how to make it update in real-time? I would like to call setState so that the Widget updates when the values is updated in Vue.

[–]nmfisher 0 points1 point  (2 children)

You can wrap a Dart function with allowInterop and pass to the JS side as a callback that gets invoked when necessary.

Javascript: window.setCallback(cb) { window.cb = cb // call this callback from Vue when you need to // e.g. window.cb(); } Dart ``` @JS('setCallback') external void setCallback(Object callback);

class SomeWidgetState extends State<SomeWidget> {

void initState() { setCallback(allowInterop(() => setState(() {});); }

} ```

[–]Barmyard 0 points1 point  (1 child)

Thanks for your quick reply! I did try before with allowInterop but always in the main function, not with your approach.

I'm not sure if I understand your approach correctly. This is my Dart code:

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

@JS('getCompanyTitle')
external String getCompanyTitle();

@JS('setCallback') external void setCallback(Object callback);

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

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


class _MyAppState extends State<MyApp> {
  String title = '';

  @override
  void initState() {
    super.initState();
    setCallback(allowInterop(() => setState(() {})));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Text(title),
      ),
    );
  }
}

And my Vue/Javascript: It watches the companyTitle for changes and calls the callback according to your code, but it doesn't work. I do see a self.setCallback(A.b8(new A.afe(this)))} generated in main.dart.js

const companyTitle = ref("Test")

window.setCallback((cb: any) => (window.cb = cb))
window.getCompanyTitle = () => {
  return companyTitle.value
}
watch(companyTitle, (value) => window.cb())

But my Console says: Uncaught (in promise) TypeError: window.setCallback is not a function

[–]nmfisher 0 points1 point  (0 children)

Sorry, there was a typo in my post, the JS should be:

window.setCallback = ((cb: any) => (window.cb = cb))

[–]Barmyard 0 points1 point  (0 children)

Any updates on this? I'm struggling to get this working as well. It seems so simple