如何解决如何在没有窗口小部件树上下文的情况下访问不同类/页面中的对象的实例?
我正在尝试从另一个没有窗口小部件树的类/页面访问AgoraIO的RtcEngine对象的实例,因此没有上下文可供Provider引用。
首先,我从此类中调用initPlatformState()来初始化RtcEngine引擎:
class Game extends StatefulWidget {
@override
_GameState createState() => _GameState();
}
class _GameState extends State<Game> implements GameListener {
@override
void initState() {
super.initState();
Agora().initPlatformState(widget.playerId);
}
}
initPlatformState通过创建一个称为engine的实例来初始化RtcEngine,我以后需要使用它来调用其他方法。该类还包含我想稍后使用相同实例来调整volume ...的方法。
class Agora {
RtcEngine engine;
// Initialize the agora app
Future<void> initPlatformState(int playerId) async {
RtcEngine engine = await RtcEngine.create(APP_ID);
}
void adjustVolume(int uid,int volume) {
engine.adjustUserPlaybackSignalVolume(uid,volume);
}
}
这是我要从其中调用AdjustVolume的类。我当时正在考虑使用Provider将实例传递给此类,但是它扩展了另一个类,并且它没有带有上下文的小部件树,因此我不确定这怎么可能或是否有更好的方法。
class Remote extends Component {
final int id;
Remote(this.id);
@override
void update() {
//this is where I'm trying to access the engine instance that was created to call adjustUserPlaybackSignalVolume method
}
}
鉴于我的情况,任何有关如何重用“引擎”实例的建议将不胜感激!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。