Ну, я делаю проект для своего T CC, и мне нужно иметь возможность загрузить изображение в хранилище Firebase и вернуть URL, чтобы иметь возможность сохранить его в хасуре, потому что там банк и более реляционные nn приходит в случае, я пробовал разные способы, но я использую Slidy Mobx и NN Я не знаю, как я это делаю.
Мой код
controller.dart
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mobx/mobx.dart';
import 'package:searchdogs/app/modules/cadastro/repository/cadastro_repository.dart';
part 'cadastro_controller.g.dart';
class CadastroController = _CadastroBase with _$CadastroController;
abstract class _CadastroBase with Store {
CadastroRepository repository;
@observable
String email = "";
@observable
String senha = "";
@observable
String name = "";
@observable
String image = "";
@observable
String endereco = "";
@observable
String telefone = "";
@observable
File teste;
@observable
bool canRegister = false;
@action
bool verifyTextField(String text){
if(text.isEmpty || text.length < 1){
return false;
}else if(text.isNotEmpty && text.length > 2){
return true;
}
}
@action
verifyIfCanRegister(){
bool emailValid = verifyTextField(email);
bool passValid = verifyTextField(senha);
bool nameValid = verifyTextField(name);
bool telefoneValid = verifyTextField(telefone);
bool enderecoValid = verifyTextField(endereco);
if(emailValid && passValid && nameValid && telefoneValid && enderecoValid){
canRegister = true;
}else{
canRegister = false;
}
}
@action
void setEmailC(String _email) => email = _email;
@action
void setSenhaC(String _senha) => senha = _senha;
@action
void setNameC(String _name) => name = _name;
@action
void setTelefoneC(String _telefone) => telefone = _telefone;
@action
void setEnderecoC(String _endereco) => endereco = _endereco;
@action
void setImageC(String _image) => image = _image;
@action
Future cadastro(BuildContext context) async {
verifyIfCanRegister();
if(canRegister == false){
print ("Erro Campo");
return;
}
try{
await repository.registerOnHasura(email, senha, name, image, endereco, telefone);
}catch(e){
String result = e.toString();
print(e);
if(result.contains("ERROR_EMAIL_ALREADY_IN_USE")){
print("Email ja esta em uso");
}else if(result.contains("Password should be at least 6 characters")){
print("Senha tem menos de 4 carecters");
}
}
}
_CadastroBase(this.repository);
}
//////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////
Repository.dart
import 'dart:ui';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:hasura_connect/hasura_connect.dart';
import 'package:searchdogs/app/modules/cadastro/cadastro_page.dart';
import 'package:searchdogs/app/shared/users_model.dart';
class CadastroRepository extends Disposable {
static const String url = "https://searchdog.herokuapp.com/v1/graphql";
final HasuraConnect connect = HasuraConnect(url);
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future cadastroWithEmail(String email, String senha) async {
AuthResult result = await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: senha);
FirebaseUser user = result.user;
return user.uid;
}
Future registerOnHasura(String email, String senha, String name, String image, String endereco,
String telefone) async {
String uid = await cadastroWithEmail(email, senha);
String imageUrl = await uploadFile(imageUrl);
String mutation = """
mutation MyMutation {
__typename
insert_users(objects: {image: "$image", name: "$name", telefone: "$telefone", uid: "$uid",
endereco: "$endereco"}) {
affected_rows
}
}
""";
var data = await connect.mutation(mutation);
return UsersModel.fromJson(data);
}
@override
void dispose() {
}
getUser(e) {}
}