Flutter Web 和 Firebase - Store/log 从 JSON API 收到的信息

Flutter Web & Firebase - Store/log information recieved from a JSON API

我想在访问者访问我的网站后立即将 log/store 从 JSON API 收到的数据发送到 Firebase。没有用户帐户。 API returns 类似于 this -only 1 post. I have implemented the data model using a JSON to Dart converter 但我对如何解析(想出如何使用 HTTP 检索它)JSON 并将其存储在 Firebase 中感到困惑。
下面是示例模型供参考。

import "dart:convert";
import "package:http/http.dart";
import "package: website/user.dar";
class Post {
  int userId;
  int id;
  String title;
  String body;

  Post({this.userId, this.id, this.title, this.body});

  Post.fromJson(Map<String, dynamic> json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    body = json['body'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['body'] = this.body;
    return data;
  }
}
class HTTP

我已经成功设置了 Firebase Analytics。目前没有使用任何其他 firebase 包。

更新:弄清楚如何在 Dart 中读取 JSON。

import 'dart:convert';
import 'http:http.dart';
class HttpService {
  static Future<IPLocation> getLocation() async {
    Response response = await get(
        "http://jsonplaceholder.typicode.com/posts/1");
    if (response.statusCode == 200) {
      final body = jsonDecode(response.body);
      Post post = IPLocation.fromJson(body);
      return post;
    } else {
      throw Exception("Failed to load Post. StatusCode: ${response.statusCode}");
    }
  }
}

现在问题仍然存在 - 将其存储在 Firebase 中的最佳方式是什么?我想将它们映射到每个访问者 - 但我无法设置用户帐户。

不确定这是否是您要查找的内容,因为我从未使用过 Firebase,但这是我过去用来从 MySQL.

中提取数据的内容
import 'dart:convert';

List<Listings> listingsFromJson(String str) =>
    List<Listings>.from(json.decode(str).map((x) => Listings.fromJson(x)));

String listingsToJson(List<Listings> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Listings{
  Listings({
    this.customerId,
    this.image,
    this.website,
  });

  String customerId;
  String image;
  String website;

  factory Listing.fromJson(Map<String, dynamic> json) => Listings(
        customerId: json["customer_id"],
        image: json["image"],
        website: json["website"],
      );

  Map<String, dynamic> toJson() => {
        "customer_id": customerId,
        "image": image,
        "website": website,
      };
}

读取获取并读取 JSON 输入。

此处为清楚起见

import "dart:convert";
import "package:http/http.dart";
import "package: website/user.dar";
class Post {
  int userId;
  int id;
  String title;
  String body;

  Post({this.userId, this.id, this.title, this.body});

  Post.fromJson(Map<String, dynamic> json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    body = json['body'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['body'] = this.body;
    return data;
  }
}

Firebase

查看使用 FirebaseAuth 设置匿名用户。然后设置并登录 userProperties。