Flutter w/Google Cloud Datastore实施 [英] Flutter w/ Google Cloud Datastore Implementation

查看:82
本文介绍了Flutter w/Google Cloud Datastore实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Flutter项目连接到我的Google云数据存储.我一直在尝试使用此处的逻辑 https://pub.dev/packages/googleapis#-example-tab- .

I'm trying to connect my Flutter project to my Google cloud datastore. I've been trying to make use of the logic from here https://pub.dev/packages/googleapis#-example-tab-.

到目前为止,我得到了以下内容:

So far I have got the below:

    String jsonCredentials = await DefaultAssetBundle.of(context).loadString("assets/data.json");
    final _credentials = new ServiceAccountCredentials.fromJson(json.decode(jsonCredentials));

    const _SCOPES = const [ds.DatastoreApi.DatastoreScope];

    auth.clientViaServiceAccount(_credentials, _SCOPES).then((http_client) {
      var datastore = new ds.DatastoreApi(http_client);
    });

以上内容只允许我连接到API,但是我一直在阅读该站点上的文档,因此不确定如何继续从数据存储中读取数据.有人可以提供任何示例吗?

The above just lets me connect to the API, however I've been reading through the documentation on that site and I'm not sure how I can then proceed to read in the data from my datastore. Can anyone provide any examples of this please?

推荐答案

有一种更好的方法,可以使用Google Cloud Plugin来实现.

There is a better way to do this using google cloud plugin for flutter.

这是链接

https://pub.dev/packages/gcloud

获得对API的访问权限使用API​​的第一步是获取经过身份验证的HTTP客户端,并通过该客户端创建用于访问不同API的API类实例.下面的代码假定您有一个名为my-project的Google Cloud Project,该项目的服务帐户凭据存储在my-project.json文件中.

Getting access to the APIs The first step in using the APIs is to get an authenticated HTTP client and with that create API class instances for accessing the different APIs. The code below assumes that you have a Google Cloud Project called my-project with credentials for a service account from that project stored in the file my-project.json.

//从文件中读取服务帐户凭据.

// Read the service account credentials from the file.

var jsonCredentials = new File('my-project.json').readAsStringSync();
var credentials = new auth.ServiceAccountCredentials.fromJson(jsonCredentials);

// Get an HTTP authenticated client using the service account credentials.
var scopes = []
    ..addAll(datastore_impl.DatastoreImpl.SCOPES)
    ..addAll(Storage.SCOPES)
    ..addAll(PubSub.SCOPES);
var client = await auth.clientViaServiceAccount(credentials, scopes);

// Instantiate objects to access Cloud Datastore, Cloud Storage
// and Cloud Pub/Sub APIs.
var db = new DatastoreDB(
    new datastore_impl.DatastoreImpl(client, 's~my-project'));
var storage = new Storage(client, 'my-project');
var pubsub = new PubSub(client, 'my-project');
All the APIs in this package supports the use of 'service scopes'. Service scopes are described in details below.

ss.fork(() {
  // register the services in the new service scope.
  registerDbService(db);
  registerStorageService(storage);
  registerPubSubService(pubsub);

  // Run application using these services.
});

阅读和写作

var person = new Person()
..name = ''
..age = 42;

等待db.commit(inserts:[person]);函数查询用于构建可运行以执行查询的Query对象.

await db.commit(inserts: [person]); The function query is used to build a Query object which can be run to perform the query.

var persons = (await db.query<Person>().run()).toList();
To fetch one or multiple existing entities, use lookup.

var key = new Person()
    ..name = 'UniqueName'
    ..age = 42
    ..parentKey = db.emptyKey;
var person = (await db.lookup<Person>([key])).single;
var people = await db.lookup<Person>([key1, key2]);

这篇关于Flutter w/Google Cloud Datastore实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆