Can I use Google sheet as data source for my mobile app

Hasan A Yousef

Using Dart language and Flutter, is there a way that I can use the data saved in my google sheet as source data for my mobile application using Dart/Flutter without downloading offline copy of the file?

Hasan A Yousef

Yes, this is possible, with the below steps:

  1. In Googlesheets:
  • Publish the sheet under consideration as csv file, using File -> Publish to the web, make sure to select the option "Automatically republish when changes are made"
  • Copy the link provided by googleSheets for the csv connectivity

enter image description here

  1. In Flutter/Dart use the below code:
  • Read data from the csv url generated above using final request = await HttpClient().getUrl(Uri.parse( 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQvf9tp4-fETDJbC-HRmRKvVFAXEAGO4lrYPpVeiYkB6nqqXdSs3CjX0eBMvjIoEeX9_qU6K2RWmzVk/pub?gid=0&single=true&output=csv'));
  • Convert the returned string into csv as rowsAsListOfValues = const CsvToListConverter().convert(csvString);
  • Display the data using DataTable(columns: const <DataColumn>[],rows: List.generate(rowsAsListOfValues.length - 1, (index) {return DataRow(cells: <DataCell>[DataCell())],
import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:io';
import 'dart:async';
import 'package:csv/csv.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _AppState createState() => _AppState();
}

Future<List<List<dynamic>>> fetchUserData() async {
  final request = await HttpClient().getUrl(Uri.parse(
      'https://docs.google.com/spreadsheets/d/e/2PACX-1vQvf9tp4-fETDJbC-HRmRKvVFAXEAGO4lrYPpVeiYkB6nqqXdSs3CjX0eBMvjIoEeX9_qU6K2RWmzVk/pub?gid=0&single=true&output=csv'));
  final response = await request.close();
  List<List<dynamic>> rowsAsListOfValues;
  await for (final csvString in response.transform(const Utf8Decoder())) {
    rowsAsListOfValues = const CsvToListConverter().convert(csvString);
  }
  return rowsAsListOfValues;
}

class _AppState extends State<HomePage> {
  List<List<dynamic>> rowsAsListOfValues;

  @override
  void didChangeDependencies() async {
    super.didChangeDependencies();
    rowsAsListOfValues = await fetchUserData();
    super.setState(() {}); // to update widget data
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            rowsAsListOfValues == null
                ? Text("Loading data...")
                : Text(
              '', // $rowsAsListOfValues
            ),
            rowsAsListOfValues == null
                ? CircularProgressIndicator()
                : DataTable(
                columns: const <DataColumn>[
                  DataColumn(
                    label: Text(
                      'City',
                      style: TextStyle(fontStyle: FontStyle.italic),
                    ),
                  ),
                  DataColumn(
                    label: Text(
                      'Branches',
                      style: TextStyle(fontStyle: FontStyle.italic),
                    ),
                  ),
                ],
                rows: List.generate(rowsAsListOfValues.length - 1, (index) {
                  return DataRow(
                    cells: <DataCell>[
                      DataCell(Text('${rowsAsListOfValues[index + 1][0]}')),
                      DataCell(Text('${rowsAsListOfValues[index + 1][1]}')),
                    ],
                  );
                })),
          ],
        ),
      ),
    );
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I write the data in Firestore to Google Sheet with app script?

How can I use my data model in my Flutter app?

How can I rearrange a Google Sheet using the data within the sheet?

How can i secure app script from the user who is going to use the google sheet?

How can I filter my search in a column on google sheet which collects data from a form?

How can I use the mite-api to automatically save data in a google sheet

Can I use my website's mobile version as an app on ios/android market?

How can I optimise this code for my Google sheet macro?

can i use google sheet formula bundled as alias?

What Google Sheet function can i use to Calculate Cash on Hand?

Can I use a field name which contains a period (".") in my data source for ag-grid?

How can I use, in Visual Studio 2017, the "C# Interactive" window to query a source in my "Data Connections"

How can i use DataTable as a data source for my test case using mstest or nunit?

How can I enable "Data Labels" in a Google Sheet via the API?

How can I secure the cloud database for my mobile app?

I can't run my mobile app on worklight development server

How can I add advanced animations to my mobile app?

Update Google Sheet Row based on ID value (I use Javascript to insert the payload to my sheet)

Can I use Google Maps API for JS on my intranet-based app?

Can I use 'com.google.android.gm.Myapp' as applicationId for my Android app?

Can I use the Google App Engine without activating my free trial?

Can I use Google sign-in in my Android app in NavigationView Items?

Can I use Google Drive as a substitute for amazon s3 for my android app?

Can i use CGo on Google App Engine?

Can i use HikariCP on Google App Engine

Can I use one NextJs api for web and mobile app?

Google sheet cannot see my app script

How can I get Terraform's 'cloudinit_config' data source to use an actual user-data script in my local directory?

Can I use JavaScript to get JSON data from the server inside my qt app?