Search code examples
fluttergoogle-drive-shared-drive

importing JSON file from google drive in flutter


I am working on app to import a lit from google drive in the form of JSON file, the App will read the JSON file only, the reason is I am not using Firestore Database is because it delays the build of the App & whatever I tried still I have errors & I can't build the APP on IOS device or simulator, so every time I will try to import the data the App will show error. my code is as the following

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Samer'),
      ),
      body: FutureBuilder<List<User>>(
        future: UsersApi.getApps(),
        builder: (context, snapshot) {
          final users = snapshot.data;
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return const Center(child: CircularProgressIndicator());
            default:
              if (snapshot.hasError) {
                return const Center(child: Text('error'));
              } else {
                return buildUsers(users!);
              }
          }
        },
      ),
    );
  }

  Widget buildUsers(List<User> users) => ListView.builder(
        physics: BouncingScrollPhysics(),
        itemCount: users.length,
        itemBuilder: (BuildContext context, int index) {
          final user = users[index];

          return Padding(
            padding: const EdgeInsets.all(15.0),
            child: Container(
              color: kOurAppsMainScreenCards,
              child: ListTile(
                title: Text(user.appName),
                // leading: CircleAvatar(
                //   backgroundImage: NetworkImage(user.imageName),
                // ),
                // subtitle: Platform.isIOS
                //     ? Text(user.paidFreeIOS)
                //     : Text(user.paidFree),
              ),
            ),
          );
        },
      );
}

and I am using http package as following:

class UsersApi {
  static Future<List<User>> getApps() async {
    final url = Uri.parse(
        'https://drive.google.com/file/d/1tAxO2kRD0NVBhefA3srbj1SKQ2l8u9Wc/view?usp=sharing');
    final response = await http.get(url);
    final body = json.decode(response.body);
    return body.map<User>(User.fromJson).toList();
  }
}

the stage thing is I inserted the same JSON file in firestore storage and the App read it.... Can somebody please help me. Regards,


Solution

  • I finally found a solution to this, the main issue is in the link itself, we have to do some modification to the link & then we can use it freely, for example: if this is the file link, we will use the bold lines in the modified link https://drive.google.com/file/d/1-T9OTLTZXTB7ydqV3tcgG4T2FQckMooB/view?usp=sharing and we have to add few words to make the link workable 100%, the following code to be added

    uc?export=view&id=
    

    the new link will be https://drive.google.com/uc?export=view&id=1-T9OTLTZXTB7ydqV3tcgG4T2FQckMooB

    Solution ref (it was for photos but it worked in JSON files also) https://youtu.be/0ZHqrf0mzrI