在我的RN应用程序中,我有以下代码。
import { PermissionsAndroid } from 'react-native';
export default new Promise(() => {
return PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS, {
title: 'Contacts List',
message: 'Canvas would like to view your contacts',
});
});
如果诺言成功与否,我想做的就是返回。然后,我可以获取承诺状态并根据该状态处理android权限。但是当前代码返回了这个。
40
:
0
_55
:
null
_65
:
0
_72
:
null
__proto__
:
Object
我在这里做什么错了?
答案 0 :(得分:1)
因为当前权限的函数是异步的,所以如果立即返回结果,则在获取结果之前将返回值。因此,建议在将值放入变量后检查该值。或者,希望在转换后以同步方式接收结果。
最后的 object
看起来像您想要的值。
示例(使用异步)
import {PermissionsAndroid} from 'react-native';
async function requestCameraPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: 'Cool Photo App Camera Permission',
message:
'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the camera');
} else {
console.log('Camera permission denied');
}
} catch (err) {
console.warn(err);
}
}
示例(使用同步)
import {PermissionsAndroid} from 'react-native';
function requestCameraPermission() {
try {
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: 'Cool Photo App Camera Permission',
message:
'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
).then(result => { console.log(result) }).catch(err => console.log(err));
} catch (err) {
console.warn(err);
}
}
答案 1 :(得分:1)
Dismissible(
key: new Key(document[i].documentID),
onDismissed: (direction) async {
await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Confirm Delete"),
content: Text("Delete item?"),
actions: <Widget>[
FlatButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop(false);
},
),
RaisedButton(
child: Text("Delete", style: TextStyle(color: Colors.white),),
onPressed: () { Firestore.instance.collection("collection").document(document[i].documentID).delete();
Scaffold.of(context).showSnackBar(
new SnackBar(content: new Text("Item deleted"),)
);
Navigator.of(context).pop(true);
},
)
],
);
}
);
},
background: Container(color: Colors.red,),
child: ...
尝试上面的代码。希望有帮助。