Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to flutter web app

After hours of searching about the topic and due to lack of documentation on Flutter Web I am asking this question.

I was trying to create a web app using flutter and had an requirement where URL such as below

website.com/user/someUserCode

would be called and an page will be launched where the data (someUserCode) will be passed to the page

but haven't got any solutions yet to resolve it.

so just rounding it all up,

How to pass and fetch the data using (get / post) methods to flutter web app?

EDIT 1

What all I know / have tried yet

I am using below code to read if some parameters are being to some class file

final Map<String, String> params = Uri.parse(html.window.location.href).queryParameters;
String data = params["userData"];

all this actually solves the Fetch part of my question (maybe)

but the part where that data will be passed to the page via URL is still missing.

EDIT 2

Since I haven't got any replies and was not able to find anything i raised an ticket on Flutter GitHub page here

anyone else looking for the same issue can track it there (if it gets resolve)

like image 218
Vicky Salunkhe Avatar asked Jan 11 '20 20:01

Vicky Salunkhe


People also ask

How do you add a variable in URL in Flutter?

var url = Uri. parse("https://yourdomain.com/videos/$id"); you need to parse the url then you can add your id any extra variable .

Can you build webapps with Flutter?

Build better web apps The web itself is a flexible platform, but Flutter is ideal for building web applications like PWAs or SPAs and bringing your existing mobile app to the web.


1 Answers

May you could do it in a easy way:

import 'dart:html';
import 'package:flutter/material.dart';
import 'home_page.dart';

void getParams() {
  var uri = Uri.dataFromString(window.location.href);
  Map<String, String> params = uri.queryParameters;
  var origin = params['origin'];
  var destiny = params['destiny'];
  print(origin);
  print(destiny);
}

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Your app',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

And then call it from browser:

http://localhost:52695/?origin=pointA&destiny=pointB

Output:

pointA
pointB 
like image 198
Cassio Seffrin Avatar answered Sep 17 '22 17:09

Cassio Seffrin