gpt4 book ai didi

dart - 自 flutter 以来调用云函数时出错

转载 作者:IT王子 更新时间:2023-10-29 07:03:40 26 4
gpt4 key购买 nike

我已经创建了它们可以工作的云函数(我用 postman 测试了它们)。当我尝试用 Flutter 调用它们时,出现错误:

I/flutter (14755): throwing firebase functions exception
I/flutter (14755): caught firebase functions exception
I/flutter (14755): INTERNAL
I/flutter (14755): Response is missing data field.
I/flutter (14755): null
I/flutter (14755): null

我已经添加了

firebase_core: ^0.3.1+1
cloud_functions: ^0.1.2+1

在我的 pubspec.yaml 中

在我的一个 Dart 文件中:

import 'package:youpa/Services/users.services.dart';

[...]

TextField(
autofocus: true,
decoration: const InputDecoration(
hintText: 'Rechercher une personne',
border: InputBorder.none,
),
onChanged: (string) async {
UsersService().searchInAllUsers(string).then((user){
print(user);
});
},
),

在这里,我的 users.services.dart :

import 'package:cloud_functions/cloud_functions.dart';
import 'package:youpa/Model/user.model.dart';
import 'dart:async';

class UsersService {
Future<User> searchInAllUsers(String search) async {
try {
final dynamic resp = await CloudFunctions.instance.call(
functionName: 'getUsersBase',
parameters: <String, dynamic>{
'txt': search,
'max': 20,
},
);
print("G : " + resp);
return resp;
} on CloudFunctionsException catch (e) {
print('caught firebase functions exception');
print(e.code);
print(e.message);
print(e.details);
} catch (e) {
print('caught generic exception');
print(e);
}
}
}

在这里,我的云功能:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
let users = [];
let number = 1;

function compareStrings(comparing, compared) {
var points = 0;
var test = compared.toUpperCase().indexOf(comparing.toUpperCase());
// console.log("Compared : " + compared + " To Upper Case : " + compared.toUpperCase());
// console.log("Comparing : " + comparing + " To Upper Case : " + comparing.toUpperCase());
// console.log(test);
if (test >= 0) {
points += 1;
if (test === 0) {
points += 2;
}
var test2 = compared.indexOf(comparing);
if (test2 >= 0) {
points += 1;
if (test === 0) {
points += 1;
}
}
if (compared.toUpperCase() === comparing.toUpperCase()) {
points += 5;
}
if (compared === comparing) {
points += 10;
}
}
return points;
}

function compareUser(firstName, lastName, pseudo, comparing) {
var points = 0;
points += compareStrings(comparing, firstName);
points += compareStrings(comparing, lastName);
points += (compareStrings(comparing, pseudo) * 4);
points += (compareStrings(comparing, firstName + " " + lastName) * 2);
return points;

}

function searchInUsers(array, comparing, max) {
console.log(max);
let list = [];
let finalList = [];
array.forEach((user) => {
const points = compareUser(user[1], user[2], user[3], comparing);
const id = user[0];
// console.log(id + " - " + user[1] + " => " + points);
// console.log(list[points]);
if (list[points] === undefined) {
list[points] = [];
}
list[points].push(id);
});
list.forEach((item, index) => {
if (index !== 0) {
item.forEach((item) => {
finalList.push(item);
});
}
});
finalList = finalList.reverse();
if(max !== undefined){
console.log("Slicing");
finalList = finalList.slice(0, max);
}
return finalList;
}

exports.getUsersBase = functions.https.onRequest((request, response) => {
if (users.length === 0) {
console.log("user = null");
db.collection('users').get()
.then((snapshot) => {
snapshot.forEach((doc) => {
const data = [];
data.push(doc.id);
data.push(doc.data().firstName);
data.push(doc.data().lastName);
data.push(doc.data().pseudo);
console.log(doc.id, '=>', doc.data());
users.push(data);
});
response.send(searchInUsers(users, request.query.txt, request.query.max));
})
.catch((err) => {
console.error('Error getting documents', err);
response.send(err);
});
} else {
console.log("user != null");
response.send(searchInUsers(users, request.query.txt, request.query.max));
}
});

当我在我的 TextField 中写入或删除一个字母时,我会在控制台中看到它:

I/flutter (14755): throwing firebase functions exception
I/flutter (14755): caught firebase functions exception
I/flutter (14755): INTERNAL
I/flutter (14755): Response is missing data field.
I/flutter (14755): null
I/flutter (14755): null

提前致谢。杰里米。

最佳答案

尝试使用:

 flutter clean
cd ios
pod install

再试一次

更新

验证您是否正确发送了参数:

    parameters: {
"data": {
'txt': search,
'max': 20,
},
},

更新 2

修改您的函数并使用这种方式获取参数:

const { body } = request;
const txt = request.query.txt || body.txt || (body.data && body.data.txt);
const max = request.query.max || body.max || (body.data && body.data.max);

更新 3

我认为你需要返回这样的东西:

  response.status(200).json({ data: {"your_data_here" }, message: "any_message_if_you_want" });

关于dart - 自 flutter 以来调用云函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55731718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com