user.dart 3.18 KB
import 'dart:core';
import 'package:flutter/material.dart';
import 'dart:convert';

User userFromJson(String str) => User.fromJson(json.decode(str));

String userToJson(User data) => json.encode(data.toJson());

class User {
  
    String email;
    String password;
    String phone;
    String token;
    String username;
    List<String> books;
    List<String> favorite;

    User(
        this.email,
        this.password,
        this.phone,
        this.token,
        this.username,
        this.books,
        this.favorite
    );
  

    User.fromJson(Map<String, dynamic> json)
    :
        token = json["token"],
        email=json["email"],
        password= json["password"],
        phone=json["phone"],
        username= json["username"],
        books=List<String>.from(json["books"].map((x) => x)),
        favorite= List<String>.from(json["favorite"].map((x) => x));
    

    Map<String, dynamic> toJson() => {
        "email": email,
        "password": password,
        "phone": phone,
        "username": username,
        "books": List<dynamic>.from(books.map((x) => x)),
        "favorite": List<dynamic>.from(favorite.map((x) => x))
    };

    void update(var user) {
    // String username, List<String> address, String phone,
    // List<String> pets, List<String> favorites
    this.email = user["email"] ?? this.email;
    this.username = user["nickname"] ?? this.username;
    //수정필요
 

    this.phone = user["phone"] ?? this.phone;
    // this._pets =
    //     user["pets"] == null ? this._pets : List<String>.from(user["pets"]);

    this.books = (user["books"] == null)
        ? this.books
        : List<String>.from(user["books"]);

    this.favorite = (user["favorite"] == null)
        ? this.favorite
        : List<String>.from(user["favorite"]);

    print("user update");
    //notifyListeners();
  }
}


//user provider
//pets provider
/*
class User with ChangeNotifier {
  String _email;
  String get email => _email;
  set email(String email) => _email = email;
  String _username;
  String get username => _username;
  set username(String username) => _username = username;

  String _phone;
  String get phone => _phone;
  set phone(String phone) => _phone = phone;
  List<String> _books = List<String>(5);
  List<String> get _books => _pets;
  List<String> _favorite = List<String>();
  List<String> get favorite => _favorites;
 

  void updatefavorite(String contentsID){
    this._favorite.add(contentsID);
  }
  void deletefavorite(String contentsID){
    this._favorite.remove(contentsID);
  }
  void update(var user) {
    // String username, List<String> address, String phone,
    // List<String> pets, List<String> favorites
    this._email = user["email"] ?? this._email;
    this._username = user["nickname"] ?? this._username;
    //수정필요
 

    this._phone = user["phone"] ?? this._phone;
    // this._pets =
    //     user["pets"] == null ? this._pets : List<String>.from(user["pets"]);

    this._books = (user["books"] == null)
        ? this._books
        : List<String>.from(user["books"]);

    this._favorite = (user["favorite"] == null)
        ? this._favorite
        : List<String>.from(user["favorite"]);

    print("user update");
    //notifyListeners();
  }
}*/