Home > Net >  Running flutter using python flask
Running flutter using python flask

Time:05-28

I'm working on a flutter project and I used python for back-end. I want to run the flutter code from my python file using os.system(). But before that I want some line code that should be executed before the os.system(). But the problem this command always run first. Is there a way to make it wait until the code is executed and then it will be running the last one or is there a another command that do the same work. Or can I make a condition in my case to let it wait.Any help is highly appreciated.

This is my code :

import json
import linecache
import os
import glob
import subprocess
from dotenv import load_dotenv
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/', methods=['POST','GET']) 
def foo():
# write into .text.txt    
    if request.method == 'POST':
        data = request.json     
        json_object = json.dumps(data)        
        files = glob.glob('var.json')
        for f in files:
            os.remove(f)
        with open('var.json', 'a') as file:
            file.write(json_object)
        envdata = json.load(open('var.json'))    
        files = glob.glob('.env')
        for f in files:
            os.remove(f)
        f = open(".env", "a")
        for key, value in envdata.items():
            f.write(f"{key.upper()}={value}\n")
        os.system("flutter run")       
    return "hello world"
    # run flutter to generate ios and apk using run methods process to run flutter app from python flutter build apk / flutter build ios. I can do it
    # zip ios and apk
    # send zip to client using email adres    
if __name__ =='__main__':
    app.run()    

CodePudding user response:

It maybe the case that the line you want to run before is a async task and that's why the os.system gets executed before it. You can add "await" keyword before the line -

await "the async line"
os.system("flutter run")

Hope it helps.

  • Related