0

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()    

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you but I'm not using a async task
what is the line you want to run?
The three line before the os.system() are not working before the os.system(). f = open(".env", "a") for key, value in envdata.items(): f.write(f"{key.upper()}={value}\n")
I add a time.sleep(10) before the os.system() and it worked is this correct ?
You know the word in the dev world - "working code is correct code" 😂. Well it will work but if sometime the for loop or the three lines took more than that time then it will stop working again.... You can do a thing call the os.system inside the for loop with a condition check as such it gets called on the last iteration of the loop. That will work in all cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.